In python, we need to store multiple data or collection data to handle large amounts of data. There are four main methods to store multiple data, including lists, tuples, sets, and dictionaries. In python, a square bracket [ ] is used to initialize a list of an element(s). A list has its own properties, and features like its elements are in an ordered way, are changeable, and may contain duplicates.
If the items of the list are treated as an element of another list, then it is referred to as a “list of lists”. We can store multiple lists in one unique list by using different methods.
We use various methods to create a Nested list or list of lists. This post serves the following learning outcomes:
- Method 1: Using the append() Method to Create a List of Lists in Python
- Method 2: Using List Initializer to Create a List of Lists in Python
- Method 3: Using a For loop Method to Create a Nested Lists in Python
- Method 4: Create a Nested List of Lists in Python Using the Comprehension Method
Now just jump into the methods one by one and discuss each in detail.
Method 1: Create a List of Lists in Python Using the append() Method
In Python, the append() method is used to create the nested list. In this method, various lists are defined that will be appended to an empty list to create a list of lists. Let’s understand it by an example:
list_1=['a','b','c'] list_2=['x','y','z'] list_3=['g','h','i'] list_of_list= list_of_list.append(list_1) list_of_list.append(list_2) list_of_list.append(list_3) print(list_of_list)
In the above code:
- Three different lists are initialized using square brackets named “list_1”, “list_2”, and “list_3”.
- After that, an empty list named “list_of_lists” is created, and we will append the three lists to that empty list.
- Lastly, the append() method adds all three lists to “list_of_list” one by one.
It is observed that all three lists are added to the “list_of_list” variable, which acts as a list of lists.
Method 2: Using List initializer Method to Create a List of Lists in Python
Python is a straightforward and easy language; the simple approach we apply to create a list of lists in python is by using List Initializer. List initializer means we use the list() function to create a list as an item of other lists. Firstly, we initialize a different list and then make a list of lists by executing the already defined list as an element. Let’s understand it by an example:
list_1=['a','b','c','d'] list_2=['x','y','z'] list_of_lists=[list_1, list_2] print(list_of_lists)
In the above code:
- Two lists are initialized using square brackets named “list_1” and “list_2”.
- After that, a new list named “list_of_lists” is created, which contains the lists “list_1” and “list_2” as its elements.
- We use the list as an element for nested lists.
In the above output, the two different lists named “list_1” and “list_2” are creating a list of lists by using the list() function.
Method 3: Using For Loop Method to Create a Nested List in Python
For Loop is also similar to the list comprehension method. In this method, an empty list is created, and the For loop cycle is initialized by giving the range. After that, a nested for loop is created to define the number of lists to be generated.
Let’s understand this via the following example:
list_of_lists =  # firstly we make list of lists for i in range(3): # now we create the append list list_of_lists.append() for j in range(3): list_of_lists[i].append(j) # print the list of lists print(list_of_lists)
In the above code:
- An empty list named “list_of_lists” is initialized.
- A for loop is utilized in which we use a range method and pass the argument value “3” to define the number of elements in a list.
- After that, the append() method is utilized to append the elements so that each list contains 3 elements.
- The For loop is utilized again to add all the elements in a single list by passing the range argument value to “3”.
- Another append() method is exercised to append the three lists.
Method 4: Using the Comprehension Method to Create a List of Lists in Python
List comprehension is the simplest method of creating a list using the existing list elements. List comprehension is surrounded by a bracket, and inside we enter an expression that is followed by for loop. The syntax of List comprehension is:
List = [expression for value in the collection]
The expression generates the elements in the list, and for loop is used to evaluate every item in the collection of data. The list comprehension method is very similar to for loop method, but the list comprehension method is better than all other iterative methods because it is a one-line statement, and the output is the same.
# Nested list comprehension list_of_lists = [[j for j in range(5)] for i in range(3)] print(list_of_lists)
In the above example, the nested for loop is utilized to create 5 elements ranging (0-4) of 3 same repeated lists. These three repeated lists are appended at the end.
The output shows the iteration of 3 same repeated lists using the list comprehension method.
In Python, the “append()” method, “For Loop” method, List initializer, and the comprehension list methods are used to create a list of lists. In the “append()” method, an empty list is created to add multiple lists into it to create a list of lists. The append() method can also be applied with the “For loop” iteratively to create a list of lists. The “Comprehension List” method adds the existing list as an element of the list of lists. While the “List Initializer” method uses the initialized list as an item of a new list. Among these, the “List initializer” method is very simple as a novel Python user can also utilize it easily.