Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (3.1k points)

1 Answer

0 like 0 dislike
by (3.1k points)
 
Best answer

Accessing values in nested list-

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]]] 

Initializing a List to a Fixed Number of Elements-

For immutable elements (e.g. None, string literals etc.):
my_list = [None] * 10
my_list = ['test'] * 10
For mutable elements, the same construct will result in all elements of the list referring to the same object, for
example, for a set:
>>> my_list=[{1}] * 10
[{1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}]
>>> my_list[0].add(2)
>>> print(my_list)
[{1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}]
Instead, to initialize the list with a fixed number of different mutable objects, use:
my_list=[{1} for _ in range(10)]

Winter 10 Days bootcamp classes(7 HRS Daily) will start from 5 & 20 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to chandwaglobal@gmail.com.

...