Create a List of Lists in Python


Lists are similar to dynamically allocated arrays, declared in other languages. Lists do not always have to be homogeneous, making Python’s most powerful feature. Integers, Strings, and Objects can all be found in a single list. Lists are changeable, which means they can be changed after they are created.

In Python, lists are ordered and counted. Every member inside the list has its separate position in the list, allowing duplication of the list’s elements while maintaining the credibility of each member. Lists help store and iterate through an iteration of data.

How to Create it?

This artifact will teach you to create and initialize a Python list of lists. A list of lists is a nested list with one or more lists. There are numerous methods for creating a list of lists. A different method will be used to construct a list of lists. Then, we will explore how to access list items after we’ve created a list of lists. Let’s look at a few examples.

Example 1: Using the append() Function to Create a List of Lists in Python in Ubuntu 20.04

The List append() function in Python is used to append and add items to the end of a List. In this illustration, we are just adding a list to a list as an item using the append() method.

Let’s see the implemented python code in the following image. In our first step, we created two list arrays represented with “list_x” and “list_y.” The lists here are initialized with different integer values.

After that, we have created a new list as “list_z,” which is initially empty. We are going to add the above lists to that list. Then, we have called the append function for the “list_z,” to which we want to append the above-initialized lists. The append function takes the list_x and the list_y as an argument individually. This append method will add the whole list in the list_z along with the elements of the list. The print function is called for printing the lists of newly generated “list_z.”

list_x = [2, 4, 6, 8]

list_y = [3, 5, 7, 9]

list_z = []

list_z.append(list_x)

list_z.append(list_y)

print(list_z)

The lists are appended to the one list as shown on the output terminal screen.

Example 2: Using the List Initializer to Create a List of Lists in Python in Ubuntu 20.04

An alternative way to make a list in Python, use the list initializer syntax. We can use this approach to make a list of lists by providing lists to the list initializer as elements.

At the beginning of the python code below, we have defined two lists separately. The lists are represented as “list1” and “list2”. These lists are stored with some integral values, as shown. Then, we have the list initializer method for which we have declared a new list as “list3”. We have initialized the “list3” with the “list1” and “list2”.

Here, We treat lists as items by using a list initializer. By using lists as items, we can make a list of lists. It’s the most straightforward method for making a list of lists. The list3 will be printed, which has both the above-specified lists.

list1 = [21, 22, 23, 24]

list2 = [25, 25, 27, 28]

list3=[list1, list2]

print(list3)

The output here shows the list of lists in the prompt shell as follows.

Example 3: Using the for-loop

We will create a more comprehensive list of lists by explicitly utilizing the append() technique with the for loop.

In the following Python script, we have established an empty list. The list is given the name “mylist.” Then, we have a for loop for creating the list of lists. We have defined the range of the lists in the list by using the range method and passed the value “3” as an argument. The for loop has the variable “m,” which will iterate over the range defined for the list.

After that, we have called the append method, which will append the three lists in the list. Now, the for loop is invoked again for adding the elements in the lists in the range of “6”. The print function will print the three lists in the list and the elements in the lists.

mylist = []

for m in range(3):

mylist.append([])

for n in range(6):

mylist[m] .append(n)

print(mylist)

The output is as follows:

Example 4: Using the List Comprehension to Create a List of Lists in Python in Ubuntu 20.04

In Python, list comprehension is a simple but elegant approach to generating lists. We generate lists using for loops and logic enclosed in square brackets using this strategy.

First of all, we have defined a list that now has string elements. Then, we created another list with the name “colors.” The list “colors” is an empty list for the time being. After that, we called the list “colors” and applied the for loop cycle to it. The for loop iterates over each item in the list and adds the elements in the list by creating a new variable, “elements.” The list “colors” of the lists will be printed at the end.

list = [‘pink’,‘red’,‘white’,‘blue’,‘green’]

colors = []

colors = [[elements] for elements in list]

print(colors)

We can also create a list of lists in python through this approach. This one is the shortest method among all.

Example 5: Accessing the Elements From the List of Lists in Python

By utilizing an index, we may access elements. The list index begins with 0 and ends with n-1, whereby n is the list length.

The following python program has a list initialized with some string values. The new empty list is also created as Animals. Then the for loop is applied to the list “Animal.” We are adding the list in the list “Animals” by using a for loop. In the end, we are displaying the list and accessing the list’s elements.

list = [‘cat’,‘dog’,‘hen’,‘parrot’, ‘panda’]

Animals = []

Animals = [[items] for items in list]

print(Animals)

print(Animals[1])

print(Animals[3])

print(Animals[4])

The list of lists is created and also accessed the list elements by the index location. The output is as follows.

Conclusion

Summing up! About creating a Python list of lists. A list of lists is a list where every value is a separate list. We have deeply gone through creating the list of lists in python. We have four different approaches that will create a list of lists in python easily and efficiently. Then, we have also accessed the list of list elements in python by the index position of the elements in the list. We hope you found it informative.



Source link