You can change each item as follows
>>> aList = [ ... [1,2,3,4,3,2], ... [2,3,4,5,4,3], ... [2,1,2,3,4,3] ... ] >>> aList[0].reverse() >>> aList[1].reverse() >>> aList[2].reverse() >>> aList [[2, 3, 4, 3, 2, 1], [3, 4, 5, 4, 3, 2], [3, 4, 3, 2, 1, 2]]
But in general it is better to use a loop, since aList can have many elements
>>> aList = [ ... [1,2,3,4,3,2], ... [2,3,4,5,4,3], ... [2,1,2,3,4,3] ... ] >>> for item in aList: ... item.reverse() ... >>> aList [[2, 3, 4, 3, 2, 1], [3, 4, 5, 4, 3, 2], [3, 4, 3, 2, 1, 2]]
Both of these methods will modify the aList in place, so the unmodified version will be destroyed. Here's how you can create a new list and leave aList unchanged
>>> aList = [ ... [1,2,3,4,3,2], ... [2,3,4,5,4,3], ... [2,1,2,3,4,3] ... ] >>> new_aList = [] >>> for item in aList: ... new_aList.append(list(reversed(item))) ... >>> new_aList [[2, 3, 4, 3, 2, 1], [3, 4, 5, 4, 3, 2], [3, 4, 3, 2, 1, 2]]
Another way to cross out the list is to use this advanced trick. -1
means step through the list with step -1
i.e. in the opposite direction.
>>> new_aList = [] >>> for item in aList: ... new_aList.append(item[::-1]) ... >>> new_aList [[2, 3, 4, 3, 2, 1], [3, 4, 5, 4, 3, 2], [3, 4, 3, 2, 1, 2]]
Instead of explicitly creating an empty list and adding to it, it is more common to write a cycle like list comprehension
>>> new_aList = [item[::-1] for item in aList] >>> new_aList [[2, 3, 4, 3, 2, 1], [3, 4, 5, 4, 3, 2], [3, 4, 3, 2, 1, 2]]