Accessing values in nested list

suggest change

Starting with a three-dimensional list:

alist = [[[1,2],[3,4]], [[5,6,7],[8,9,10], [12, 13, 14]]]

Accessing items in the list:

print(alist[0][0][1])
# 2
# Accesses second element in the first list in the first list

print(alist[1][1][2])
# 10
# Accesses the third element in the second list in the second list

Performing support operations:

alist[0][0].append(11)
print(alist[0][0][2])
# 11
# Appends 11 to the end of the first list in the first list

Using nested for loops to print the list:

for row in alist: #One way to loop through nested lists
    for col in row:
        print(col)
# [1, 2, 11]
# [3, 4]
# [5, 6, 7]
# [8, 9, 10]
# [12, 13, 14]

Note that this operation can be used in a list comprehension or even as a generator to produce efficiencies, e.g.:

[col for row in alist for col in row]
# [[1, 2, 11], [3, 4], [5, 6, 7], [8, 9, 10], [12, 13, 14]]

Not all items in the outer lists have to be lists themselves:

alist[1].insert(2, 15)
# Inserts 15 into the third position in the second list

Another way to use nested for loops. The other way is better but I’ve needed to use this on occasion:

for row in range(len(alist)): #A less Pythonic way to loop through lists
    for col in range(len(alist[row])):
       print(alist[row][col])

# [1, 2, 11]
# [3, 4]
# [5, 6, 7]
# [8, 9, 10]
# 15
# [12, 13, 14]

Using slices in nested list:

print(alist[1][1:])
# [[8, 9, 10], 15, [12, 13, 14]]
# Slices still work

The final list:

print(alist)
# [[[1, 2, 11], [3, 4]], [[5, 6, 7], [8, 9, 10], 15, [12, 13, 14]]]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


List:
* List
* Accessing values in nested list

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve