
Hello Techies,
In this tutorial, we will learn about How to create or access a list of lists in Python with the help of examples.
Table of Contents
Different ways to create, access, and use of a list of lists in Python
What is a List of Lists?
The list of lists in Python is to add a list to another list. We can also call this a Nested List.
Example: list_of_list = [[1, 2, 3, 4], [5, 6, 7, 8]]
Append method to create a list of lists in Python
Creating a list of lists in Python is easy. You need to use the append method to create list of lists.
Below is a simple example to create a list of lists in Python.
# Program to create a list of lists in Python list1 = [1, 2, 3, 4] list2 = [5, 6, 7, 8] # create empty list list_of_lists = [] list_of_lists.append(list1) list_of_lists.append(list2) print("List of Lists:", list_of_lists)
Output:
List of Lists: [[1, 2, 3, 4], [5, 6, 7, 8]]
How to access elements in a list of lists in Python
There are many ways that you can access the elements of a list. You know that the Python list starts with index 0 and is written in square braces. So we can access the list in 2 ways: using the positive index and negative index.
Example of Positive indexing
# Program to access elements in a list of lists in Python # using positive indexing list_of_list = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] # accessing second element from list_of_list object second_obj_lol = list_of_list[1] print("Second element of list is:", second_obj_lol) # accessing 3rd element of 1st list from list_of_list object third_ele_lol = list_of_list[0][2] print("3rd element of 1st list is:", third_ele_lol)
Output:
Second element of list is: [5, 6, 7, 8] 3rd element of 1st list is: 3
Example of Negative indexing
# Program to access elements in a list of lists in Python # using negative indexing list_of_list = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] # accessing second element from list_of_list object second_obj_lol = list_of_list[-2] print("Second element of list is:", second_obj_lol) # accessing 3rd element of 1st list from list_of_list object third_ele_lol = list_of_list[-3][-2] print("3rd element of 1st list is:", third_ele_lol)
Output:
Second element of list is: [5, 6, 7, 8] 3rd element of 1st list is: 3
Remove items from a List of Lists
You can use the below methods to remove elements from the list of lists:
- pop()
- remove()
- del keyword
pop()
The pop() function modifies the list and returns the removed item.
# Program to remove an element from List of Lists # using pop() function list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # pop() function remove element from list result = list1[1].pop(1) print("Removed element is:", result) print("Modified list is:", list1)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Removed element is: 6 Modified list is: [[1, 2, 3, 4], [5, 7, 8], [4, 8, 9, 0]]
remove()
If you’re not sure where the item in the list is, use the remove() method to delete it by value.
# Program to remove an element from List of Lists # using remove() function list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Originaremovel list is:", list1) # remove() method remove element from list result = list1[1].remove(8) print("Modified list is:", list1)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Modified list is: [[1, 2, 3, 4], [5, 6, 7], [4, 8, 9, 0]]
del
Use the del statement if you do not need the removed value.
# Program to remove an element from List of Lists # using del keyword list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # del keyword to remove element from list del list1[1][1] del list1[2] print("Modified list is:", list1)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Modified list is: [[1, 2, 3, 4], [5, 7, 8]]
Iterate through a Nested List
To iterate the items on the list of lists, use a simple for loop.
# Program to Iterate through a Nested List list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] # Iterate through a Nested List for obj in list1: for each in obj: print(each, end=' ')
Output:
1 2 3 4 5 6 7 8 4 8 9 0
Find Nested List Length
You can use the built-in len() function to find the length of the nested list in Python.
# Program to find length of the nested list list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Length of list is", len(list1)) print("Length of 2 element list is", len(list1[1]))
Output:
Length of list is 3 Length of 2 element list is 4
Convert List of Lists to One List using for loop
We’re using a for loop with the append() method to convert a list to a list. Check out the following example:
# Program to Convert a List of Lists to One List using for loop list_of_list = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] new_list = [] for each in list_of_list: for i in each: new_list.append(i) print("Result is:", new_list)
Output:
Result is: [1, 2, 3, 4, 5, 6, 7, 8, 4, 8, 9, 0]
Convert List of Lists to One List using List Comprehension
In the example below, we are using the list comprehension method to convert List of Lists to One List.
# Program to Convert List of Lists to One List # using list comprehension list_of_list = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list_of_list) # list comprehension one_list = [item for sublist in list_of_list for item in sublist] print("Result is:", one_list)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Result is: [1, 2, 3, 4, 5, 6, 7, 8, 4, 8, 9, 0]
Convert List of Lists to One List using itertools (chain())
itertools is a Python 3 library. Which has a lot of great functions. To convert a list of lists into a list, we are using one of its functions which is a chain() function.
The chain() function takes multiple iterations as parameters and returns a single iteration.
Example of chain() function in Python to convert List of lists in one list
# Program to Convert List of Lists to One List # using itertools(chain()) import itertools list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # using chain() function that will to print all elements of lists one_list = list(itertools.chain(*list1)) print("Result is:", one_list)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Result is: [1, 2, 3, 4, 5, 6, 7, 8, 4, 8, 9, 0]
Flatten List of Lists Using the sum() function
To Flatten List of Lists using Python’s Built-In sum() function. Check out the below example:
# Program to Convert List of Lists to One List # using sum() function list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # using sum() function that will to print all elements of lists one_list = sum(list1, []) print("Result is:", one_list)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Result is: [1, 2, 3, 4, 5, 6, 7, 8, 4, 8, 9, 0]
Flatten List of Lists Using Lambda
An anonymous function can be defined using the Lambda keyword. Let’s use the lambda function to convert a list of lists into a list.
Example:
# Program to Convert List of Lists to One List # using lambda function list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # using lambda function # to print all elements of lists one_list = lambda list1: [item for sublist in list1 for item in sublist] print("Result is:", one_list(list1))
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Result is: [1, 2, 3, 4, 5, 6, 7, 8, 4, 8, 9, 0]
In the example below, we are using Lambda with reduce() function to convert a list of items into a list.
# Program to Convert List of Lists to One List # using lambda with reduce() function from functools import reduce # python 3 list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # using lambda with reduce() function # to print all elements of lists one_list = reduce(lambda x, y: x+y, list1) print("Result is:", one_list)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Result is: [1, 2, 3, 4, 5, 6, 7, 8, 4, 8, 9, 0]
Convert List of Lists to Dictionary
Example of a list of lists to dictionary conversion in Python
# Program to Convert List of Lists to the dictionary list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # list to dict conversion result = {item[0]: item[1:] for item in list1} print("Result is:", result)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Result is: {1: [2, 3, 4], 5: [6, 7, 8], 4: [8, 9, 0]}
Convert List of Lists to Tuple
Example of a list of lists to Tuple conversion in Python
# Program to Convert List of Lists to a tuple list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] print("Original list is:", list1) # list to tuple conversion result = tuple(tuple(sub) for sub in list1) print("Result is:", result)
Output:
Original list is: [[1, 2, 3, 4], [5, 6, 7, 8], [4, 8, 9, 0]] Result is: ((1, 2, 3, 4), (5, 6, 7, 8), (4, 8, 9, 0))
FAQ on list of lists in Python
How do I make a list in a list Python?
Creating a list of lists in Python is easy. You need to use the append method to create list in a list.
How do you access lists inside a list?
We can use the index method to access list inside a list.
What is a nested list in Python?
Nested list is nothing but a list of lists.
Conclusion
In this tutorial, you learned about a list of lists in Python with the help of multiple examples, We have covered the following points:
- What’s a List of Lists?
- Append method to create a list of lists in Python
- How to access elements in a list of lists in Python
- Remove items from a List of Lists
- Iterate through a Nested List
- Find Nested List Length
- Convert List of Lists to One List using for loop
- Convert List of Lists to One List using List Comprehension
- Convert List of Lists to One List using itertools (chain())
- Flatten List of Lists Using the sum() function
- Flatten List of Lists Using Lambda
- Convert List of Lists to Dictionary
- Convert List of Lists to Tuple
