Issue
# Function that takes array as parameter and returns a reverse array
# using loop
def reverse_list(arr = []):
for i in arr[::-1]:
print(i)
arr_num = [1, 2, 3, 4, 5]
print(reverse_list(arr_num))
I want my function to take an array as a parameter, but I'm not sure if the structure /code of it is correct.
Solution
Simply create a new array inside the function and either append each values before returning it
def reverse_list(arr = []):
new_arr = []
for i in arr[::-1]:
# print(i)
new_arr.append(i)
return new_arr
arr_num = [1, 2, 3, 4, 5]
print(reverse_list(arr_num))
Or in short assign the reversed array to the new array
def reverse_list(arr = []):
new_arr = arr[::-1]
return new_arr
Answered By - tebkanlo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.