Nested data structures allow you to organize data in multiple levels, enabling more complex data modeling required for real-world applications. Specifically, lists of lists are a common way to implement nested structures in Python.
In this comprehensive guide, we will explore the creation, manipulation, and access of nested lists in Python. We will cover the following topics in detail with example code snippets:
Table of Contents
Open Table of Contents
What are Nested Lists and Why Use Them?
Nested lists are lists that contain other lists as elements. For example:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Here the main list contains 3 sublists inside it. The nesting can go to multiple levels to model hierarchical data.
Nested lists are useful for organizing related data together. For example, we can store information about multiple users in a nested list:
user_data = [
["John", 28, "[email protected]"],
["Mary", 32, "[email protected]"],
["Steve", 40, "[email protected]"]
]
Here each sublist contains the name, age and email of a user. This keeps all the data about each user together for easy access.
We can model complex real-world data like game boards, matrices, trie trees etc. using nested lists in Python. The ability to nest lists inside lists enables efficient data modeling for many problems.
Creating Nested Lists
We can create nested lists by simply putting lists inside other lists:
nested_list = [[1, 2, 3], [4, 5, 6]]
This creates a list with 2 sublists inside it.
We can also create nested lists using list comprehensions:
nested_list = [[i for i in range(3)] for j in range(2)]
print(nested_list)
# Output: [[0, 1, 2], [0, 1, 2]]
This comprehension creates a 2x3 nested list by iterating through the range.
To initialize a multidimensional nested list with default values, we can nest list comprehensions:
matrix = [[0 for i in range(3)] for j in range(3)]
print(matrix)
# Output: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
This creates a 3x3 matrix fully populated with 0s.
We can also use the append()
method to build up nested lists:
nested_list = []
for i in range(3):
nested_list.append([i])
print(nested_list)
# Output: [[0], [1], [2]]
This gradually builds up the nested list by appending new sublists.
Accessing Elements from Nested Lists
To access nested list elements, we use two indices separated by a comma. The first index selects the sublist, and the second index gets an element from that sublist.
For example:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# First element of 2nd sublist
print(nested_list[1][0])
# Third element of 1st sublist
print(nested_list[0][2])
We can also use negative indices to access elements from the back.
To extract a full sublist, just use a single index without a second one:
sublist = nested_list[1] # 2nd sublist
We can iterate through the outer list to process each sublist:
for sublist in nested_list:
print(sublist)
And then iterate through each sublist as needed:
for sublist in nested_list:
for item in sublist:
print(item)
This double for loop structure is very common for processing nested lists in Python.
Slicing Nested Lists
We can slice nested lists to extract sections just like regular lists.
To slice the outer list, use a single slice:
nested_list[:2] # First two sublists
To slice a sublist, add a second slice:
nested_list[1][:2] # First two of second sublist
We can combine slicing and indexing to extract specific ranges from the sublists.
nested_list[1][1:3] # Second and third of second sublist
All the usual slice notation works like reversing, strides etc.
Modifying Nested Lists
We can modify nested lists using index assignments:
nested_list[1][2] = 10 # Assign new value
This lets us modify any element by accessing it directly.
We can also insert new sublists:
nested_list.insert(1, [10, 11, 12]) # Insert at index 1
And modify multiple elements using slice assignment:
nested_list[1][:2] = [8, 9] # Modify first two of sublist
This is useful for bulk-replacing slice ranges.
We can also use del
to remove elements:
del nested_list[1][0] # Delete first of second sublist
And append()
/extend()
work like normal lists:
nested_list[0].append(4) # Append to first sublist
So all the usual list operations can be used on the sublists.
Deleting Elements from Nested Lists
To delete an entire sublist, use del
with a single index:
del nested_list[1] # Delete second sublist
This removes the entire sublist at index 1.
To delete a single element, use the double index:
del nested_list[0][1] # Delete second element of first sublist
We can also delete slices using del
:
del nested_list[0][1:3] # Delete a slice from sublist
And to clear a list, assign an empty list:
nested_list = [] # Clear entire nested list
Looping Through Nested Lists
We can iterate through nested lists using for loops:
To loop through each sublist:
for sublist in nested_list:
print(sublist)
To loop through each individual element:
for sublist in nested_list:
for item in sublist:
print(item)
We can also use while
loops:
i = 0
while i < len(nested_list):
j = 0
while j < len(nested_list[i]):
print(nested_list[i][j])
j += 1
i += 1
This manually indexes through each element.
We can use enumerate()
to get the indices:
for i, sublist in enumerate(nested_list):
for j, item in enumerate(sublist):
print(i, j, item)
So loops provide a flexible way to iterate through nested lists in Python.
Operations on Nested Lists
Flattening
Flattening reduces a nested list to a single flat list of elements.
We can use list comprehensions:
nested_list = [[1, 2], [3, 4], [5, 6]]
flat_list = [item for sublist in nested_list for item in sublist]
print(flat_list)
# Output: [1, 2, 3, 4, 5, 6]
Or the sum()
function:
flat_list = sum(nested_list, [])
print(flat_list)
# Output: [1, 2, 3, 4, 5, 6]
This is useful for processing the elements independently.
Concatenation
We can concatenate nested lists using +
:
list1 = [[1, 2]]
list2 = [[3, 4]]
combined = list1 + list2
print(combined)
# Output: [[1, 2], [3, 4]]
This joins the nested lists together into one combined nested list.
Repetition
Nested list repetition with *
duplicates the nested structure:
nested_list = [[1], [2]]
doubled = nested_list * 2
print(doubled)
# Output: [[1], [2], [1], [2]]
This repeats each sublist rather than just duplicating the elements.
Checking Equality
We can check if two nested lists are equal using ==
:
list1 = [[1, 2], [3, 4]]
list2 = [[1, 2], [3, 4]]
print(list1 == list2) # True
This compares the nested structure and values element-wise.
So the usual list operations work intuitively on nested lists in Python.
Use Cases for Nested Lists
Some common use cases for nested lists in Python include:
-
Representing multi-dimensional arrays and matrices for numerical computing. For example, 2D arrays in linear algebra.
-
Storing grid-based data like game boards for games like chess or sudoku.
-
Implementing tree structures with each subtree stored in a sublist. For example, binary search trees.
-
Recursive data structures like directories of folders and sub-folders.
-
Modeling networks with nodes and edges as sublists. Useful for graph theory problems.
-
Natural language processing tasks like representing sentences composed of words.
-
Storing tabular data with related columns and rows in sublists.
-
Grouping common data like categories of products or user profiles together.
Nested lists provide a simple but powerful way to model hierarchical and multidimensional data for complex Python programs. The nested structure leads to efficient lookup and access.
Conclusion
This guide covered the key aspects of using lists of lists to create nested data structures in Python. We looked at the syntax for creation, access, slicing, modification, deletion and looping over nested lists. We also explored common operations like flattening and use cases like matrices.
The ability to nest lists inside other lists enables clean and efficient data modeling. You can use these techniques for organizing related information in hierarchical relationships like directories, family trees, network topologies etc. Nested lists are a versatile data structure that every Python programmer should understand.