How to Create a List of N-Lists in Python
Last Updated :
23 Jul, 2025
In Python, we can have a list of many different kinds, including strings, numbers, and more. Python also allows us to create a nested list, often known as a two-dimensional list, which is a list within a list. Here we will cover different approaches to creating a list of n-lists in Python.
The different approaches that we will cover in this article are:
Creating a list of N-lists in Python, each with a different memory location
To achieve distinct memory locations for each sublist, you should create each sublist independently. Here are a few methods to do this:
Example 1: Using List comprehension
In this example, we are using list comprehension for generating a list of lists.
Python
# Create a list of 6 independent sublists using list comprehension
d1 = [[] for x in range(6)]
print("Initial list:", d1)
# Print memory addresses of each sublist
for i in range(len(d1)):
print(f"Memory address of d1[{i}]:", id(d1[i]))
# Modify the first sublist by appending the value 2
d1[0].append(2)
print("Modified list:", d1)
# Print memory addresses again to confirm they haven't changed
for i in range(len(d1)):
print(f"Memory address of d1[{i}] after modification:", id(d1[i]))
Output[[], [], [], [], [], []]
[[2], [], [], [], [], []]
Example 2: Using a loop
In this example, we are creating a list using a loop with a range of 6 and appending lists into a list.
Python
N = 5
lists = []
# Create a list with N independent empty sublists
for _ in range(N):
lists.append([])
# Print initial state
print("Initial lists:", lists)
# Print memory addresses of each sublist
for i in range(len(lists)):
print(f"Memory address of lists[{i}]:", id(lists[i]))
# Modify the first sublist by appending the value 2
lists[0].append(2)
# Print modified state
print("Modified lists:", lists)
# Print memory addresses again to confirm they haven't changed
for i in range(len(lists)):
print(f"Memory address of lists[{i}] after modification:", id(lists[i]))
Output[[], [], [], [], []]
[[2], [], [], [], []]
Creating a list of N-lists in Python, each with a same memory location
Creating a list of N-lists in Python, each with a different memory location, involves ensuring that each sublist is a distinct object. This is important to avoid unintended side effects where modifying one sublist affects others.
Example 1: Using simple multiplication
In this example, we are multiplying the list by 4 to get a list of lists.
Python
# Create a list with 4 references to the same sublist
lis = [[]] * 4
print("Initial list:", lis)
# Print memory addresses of each sublist
for i in range(len(lis)):
print(f"Memory address of lis[{i}]:", id(lis[i]))
# Modify the first sublist
lis[0].append(2)
print("Modified list:", lis)
# Print memory addresses again to confirm they haven't changed
for i in range(len(lis)):
print(f"Memory address of lis[{i}] after modification:", id(lis[i]))
Output[[], [], [], []]
[[2], [2], [2], [2]]
Example 2: Using itertools
Using the built-in repeat function from the itertools module. This function allows you to repeat a given object a certain number of times, which can be useful for creating lists of lists.
Here is an example of how you can use repeat to create a list of lists in Python:
Python
from itertools import repeat
# Create a list of lists with 5 sub-lists using itertools.repeat
n_lists = list(repeat([], 5))
print("Initial list:", n_lists)
for i in range(len(n_lists)):
print(f"Memory address of n_lists[{i}]:", id(n_lists[i]))
# Modify the first sublist
n_lists[0].append(2)
print("Modified list:", n_lists)
# Print memory addresses again to confirm they haven't changed
for i in range(len(n_lists)):
print(f"Memory address of n_lists[{i}] after modification:", id(n_lists[i]))
Output[[], [], [], [], []]
[[2], [2], [2], [2], [2]]
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice