How to Get a Reversed Copy of a List (Avoid a Separate Statement When Chaining a Method After .Reverse)

How can I get a reversed copy of a list (avoid a separate statement when chaining a method after .reverse)?

You can use reversed(formation) to return a reverse iterator of formation. When you call formation.reverse() it does an in place reversal of the list and returns None.

EDIT:

I see what you are trying to do now, in my opinion it's easier to just do this with a list comprehension:

def solution(formation):
return len([k for k in formation[formation.index(bCamel)+1:] if k == fCamel]) == 0

This basically looks at all the elements after the first bCamel and collects all the elements that have the value fCamel. If that list has a length == 0 you have a solution.

Here's a few examples:

>>> k = ['F','F','B','B','F']
>>> solution(k)
False
>>> k = ['F','F','B','B','B']
>>> solution(k)
True
>>> k = ['F','F','B','F','F','B','B']
>>> solution(k)
False
>>>

Reverse list method print None for a list returned in a function?

list.reverse() mutates the list in-place and returns None. reverse leaves the original list alone and returns a reversed version of it.

Python List Reverse Function

Because reverse() reverses the list in place, it returns none.

Try this.

list_10 = [1,10,20,4,40,14]
list_10.reverse()
list_11 = list_10

why list reversing returns None?in python

Because reverse does not return a list, it mutates the list in place. A method with no return always returns None. Try:

ls = [1,2,3,4,5,6,7,8,9]
print('List = ',ls)
ls.reverse()
print('Reversed List = ',ls)

Alternatively you could use a step value with slice notation. This also has the effect of not mutating the input list:

ls = [1,2,3,4,5,6,7,8,9]
print('List = ',ls)
print('Reversed List = ',ls[::-1])

Reverse a list of string giving me none

reverse

You got to copy the list and then reverse it with l.reverse(), not l = n.reverse().

copy

You have to use copy, not just l = n, because if you do so you will have two reference to the same data, so that when you reverse l you reverse also n. If you do not want to reverse also the original n list, you have to make a brand new copy with l = n. copy, then you reverse l, without modifying the original data stored with n label. You can copy a list also with l = n[:].

>>> s = "12345"
>>> n = list(s)
>>> l = n.copy() # or l = n[:]
>>> l.reverse()
>>> n
['1', '2', '3', '4', '5']
>>> l
['5', '4', '3', '2', '1']
>>>

Thake a list and get the items in a string

If you want a string back

>>> k = "".join(l)
>>> k
'54321'

Why does Python return None on list.reverse()?

What I'm about to write was already said here, but I'll write it anyway because I think it will perhaps add some clarity.

You're asking why the reverse method doesn't return a (reference to the) result, and instead modifies the list in-place. In the official python tutorial, it says this on the matter:

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.

In other words (or at least, this is the way I think about it) - python tries to mutate in-place where-ever possible (that is, when dealing with an immutable data structure), and when it mutates in-place, it doesn't also return a reference to the list - because then it would appear that it is returning a new list, when it is really returning the old list.

To be clear, this is only true for object methods, not functions that take a list, for example, because the function has no way of knowing whether or not it can mutate the iterable that was passed in. Are you passing a list or a tuple? The function has no way of knowing, unlike an object method.

How to reverse a list without modifying the original list in Python

you need to make a copy of your list

L=[1,2,3]
# R=L
R = L[:]
L.reverse()

or more directly (reversing using slice notation):

R = L[::-1]

if you just write R = L then R is just a new reference on the same list L.

if you also need to copy the elements in your list, use copy.deepcopy; R = L[:] only produces a shallow copy (which is fine in your case where there are only ints in the list).

How do I reverse a list or loop over it backwards?

To get a new reversed list, apply the reversed function and collect the items into a list:

>>> xs = [0, 10, 20, 40]
>>> list(reversed(xs))
[40, 20, 10, 0]

To iterate backwards through a list:

>>> xs = [0, 10, 20, 40]
>>> for x in reversed(xs):
... print(x)
40
20
10
0


Related Topics



Leave a reply



Submit