I play with python 2.7. Here’s my problem:
>>> bra=[]
>>> put=['a','t']
>>> bra.append(put)
>>> bra
[['a', 't']]
>>> bra.append(put)
>>> bra
[['a', 't'], ['a', 't']]
>>> bra.append(put.reverse())
>>> bra
[['t', 'a'], ['t', 'a'], None]
My question is: why does de python interpreter give that result in the last line, instead this:
[['a', 't'], ['a', 't'], ['t', 'a']]
or how can I get this result?
list.reverse()modifies the list in-place, and doesn’t return anything, which is equivalent to returningNone.You need
reversed(put). This function indeed returns an inversed version of the iterable.