4

I'm having trouble wrapping my head around dealing with lists within lists in python (I am rather new at programming).

Now how would I access each nested list position and then manipulate the position of the nested list's values to, for example, change their order while maintaining the position of the nested list in the main list.

For example: if I wanted to go from:

aList = [
    [1,2,3,4,3,2],
    [2,3,4,5,4,3],
    [2,1,2,3,4,3]
]

to here:

new_aList = [
     [2,3,4,3,2,1],
     [3,4,5,4,3,2],
     [3,4,3,2,1,2]
]

I get how to access each list, but I'm experiencing a block as to how I would change the position of the values of the nested lists.

Thanks for any help!

4 Answers 4

9

Use a list comprehension to get the new elements, and optionally slice-assign to replace the existing elements in the list.

new_aList = [list(reversed(x)) for x in aList]
aList[:] = [list(reversed(x)) for x in aList]
Sign up to request clarification or add additional context in comments.

2 Comments

For someone new at programming, list comprehensions do not assist in comprehension. The answer is correct, but cryptic.
reversed returns a listreverseiterator you should use list(reversed(x)) instead
4

You could reverse each item like this

>>> 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's better to use a loop since aList could have lots of items

>>> 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 those methods will modify aList in place, so the unmodified version is destroyed. Heres how you could 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 reverse a list is to use this extended slice trick. The -1 means step through the list in steps of -1 ie. backwards.

>>> 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 making an empty list and appending to it, it is more usual to write a loop like this as a 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]]

1 Comment

Thanks this is super helpful. I like the extended slice trick
2
bList = []
for l in aList:
  l.reverse()
  bList.append(l)

3 Comments

You forgot to call the .reverse() method.
ah! sorry i was in the middle of reading my ruby book - forgot the parens
Also, the method returns None.
1

For more fine-grained manipulation of lists, the position of values can be swapped using tuple unpacking (which neatly avoids having to use temporary variables):

aList[0][1], aList[0][3] = aList[0][3], aList[0][1]

After this operation, the second and fourth values would change places, and so aList[0] would look like this:

[1, 4, 3, 2, 3, 2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.