. Advertisement .
..3..
. Advertisement .
..4..
Constructing a list is perhaps no longer strange for some people, but a little dare to say the same thing about their knowledge regarding creating a list of lists in Python.
So, what is it, and how does it work for your big math? Read on for no more quizzes not yet resolved.
What Is A List Of Lists In Python?
Let’s recall some Python list-related comprehensions. To begin with, a list in Python is a changeable and organized collection that enables a member to be repeated twice.
So, how about a List of lists? It is simply a list that contains many other lists as its components.
You can put any type of element into a Python List, and so does we get a Python List of Lists. As a result, these different kinds of lists will have completely no trouble playing as members that construct a list of lists in Python.
For example, here goes the following Python List and List of Lists:
Python List: [5, 4.2, 69, 'car', 'False']
Python List of Lists: [ [5, 4.2, 69, 'car', 'False'], [6, 'bike', 87, 'Orange'], [7, 98, 'pupil', 'True']
To Create A List Of Lists In Python
There are four distinct ways to create a List of Lists in Python as below:
Use The for Loop
Say we wish to build a list with three various sub-lists. Then how to do so?
The first thing is to construct an empty list, using “list = []”. Next, we use a for loop to cycle from 0 to 2, then appending an empty list to the new list after each iteration would be the last step to arrive.
Running the code
list = []
for a in range(4):
list.append([])
for b in range(3):
list[a].append(b)
print(list)
Output
[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]
Use list.append()
Another method to come is to use list_of_lists.append(). That way, all you have to get done is calling list_of_lists.append(object) with the object is a list to conjoin an object to list, and list is a content-free list.
Running the code
list_of_lists = []
list_of_lists.append([7])
list_of_lists.append([7, 7])
list_of_lists.append([7, 7, 7])
list_of_lists.append([7, 7, 7, 7])
list_of_lists.append([7, 7, 7, 7, 7])
list_of_lists.append([7, 7, 7, 7, 7, 7])
print(list_of_lists)
Output
[[7], [7, 7], [7, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7, 7], [7, 7, 7, 7, 7, 7]]
Use List Comprehension & repeat() Function
For those who have not yet acquired, the itertools module in Python does supply a repeat() function.
Running the code
itertools.repeat(object[, N])
from itertools import repeat
num = 5
list_of_lists = [[] for x in repeat(None, num)]
print('List of lists:')
print(list_of_lists)
As such, “itertools.repeat(object[, N])” aims to return an Iterator, which iterates across the specified object N times. After that, it may be used for simple iterations from 0 to 4, with each iteration adding a sublist to the major list.
Output
List of lists:
[[], [], [], [], []]
Once you’ve confirmed that it returned a five-sub-list-level list, you also can check to verify if each sub-list has its own item.
Running the code
for elem in list_of_lists:
print(id(elem))
Output
200792264
200792232
200792296
200792168
200740648
The result above demonstrates that each sublist has a unique identity. Even though this is one of the less often used methods for creating a list of lists, it is still beneficial in one way or another to learn new things.
Just give it a thought: Once you understand the APIs and their use cases, you can apply them to a variety of other circumstances.
Use A List Comprehension & Range() Function
Thanks to the range() feature of Python, we are able to produce a series of numbers ranging from 0 to n-1. Then, it will help us create and add a sub-list to the major list for each entry in the series by the use of List.
Running the code
list_of_lists = [[1] for i in range(6)]
print('List of lists:')
print(list_of_lists)
Output
List of lists:
[[1], [1], [1], [1], [1], [1]]
After guaranteeing that it generated a five-level list, here is what to do to see whether each sub-list has a separate item.
Keep running this code
for elem in list_of_lists:
print(id(elem))
Output
1635607606080
1635607646656
1635607606400
1635607909504
1635607606528
1635607652032
Now, we grasp for sure that there are indeed differences among those sublists!
Conclusion
Above are chiefly all that is needed to learn about how to create List of Lists in Python. Make sure you’ve gone through this article thoroughly so these insights can be of great use.
Also, don’t hesitate to leave your comment if there is anything that confuses you!
Leave a comment