Skip to content Skip to sidebar Skip to footer

Returning The List Reversed

i have this question: Write a function reverse3(nums) that takes a list of ints of length 3 called nums and returns a new list with the elements in reverse order, so [1, 2, 3] beco

Solution 1:

reversed returns an iterator. If you want to get a list object, use list:

>>>defreverse3(nums):...returnlist(reversed(nums))...>>>reverse3([4,5,6])
[6, 5, 4]

Solution 2:

List has a built in reverse function

list.reverse()

so you could use

nums.reverse()

However, if you want to write your own function you would need to do a reverse iteration to another list.

Something like-

defrevList(nums)
   i = len(mums) -1while i >= 0:
      revNums[i] = nums.pop()
      i = i - 1return(revNums)

Post a Comment for "Returning The List Reversed"