How to Create a List of N-Lists in Python?

When working with data in Python, there may be situations where you need to organise your data into a list of N?lists. This data structure, commonly known as a "list of lists," allows you to store and access multiple sets of data in a structured and flexible manner. Each individual list within the main list can contain different types of data or even other nested lists.

Whether you're dealing with multidimensional datasets or complex data structures, understanding how to create and work with a list of N?lists will enhance your Python programming skills and enable you to handle diverse data scenarios more effectively.

Using Simple Multiplication (Warning: Shared References)

This approach creates multiple references to the same list object. Be careful modifying one sublist affects all others ?

n = 3  # Number of sublists
m = 4  # Number of elements in each sublist

# Creating the list of N-lists using simple multiplication
nested_list = [[]] * n

# Displaying the nested list
print("Initial:", nested_list)

# Adding an element to demonstrate shared reference issue
nested_list[0].append("test")
print("After modifying one sublist:", nested_list)
Initial: [[], [], []]
After modifying one sublist: [['test'], ['test'], ['test']]

Important: This approach creates multiple references to the same list object, so modifying one sublist affects all others.

Using a Loop

This approach creates independent sublists by iterating and creating new list objects ?

n = 3  # Number of sublists

# Creating the list of N-lists using a loop
nested_list = []
for _ in range(n):
    sublist = []
    nested_list.append(sublist)

# Displaying the nested list
print("Initial:", nested_list)

# Testing independence of sublists
nested_list[0].append("test")
print("After modifying one sublist:", nested_list)
Initial: [[], [], []]
After modifying one sublist: [['test'], [], []]

Using List Comprehension

The most Pythonic approach using list comprehension to create independent sublists ?

n = 3  # Number of sublists

# Creating the list of N-lists using list comprehension
nested_list = [[] for _ in range(n)]

# Displaying the nested list
print("Initial:", nested_list)

# Testing independence of sublists
nested_list[0].append("test")
print("After modifying one sublist:", nested_list)
Initial: [[], [], []]
After modifying one sublist: [['test'], [], []]

Using itertools.repeat() (Warning: Shared References)

Similar to simple multiplication, this creates shared references to the same object ?

import itertools

n = 3  # Number of sublists

# Creating the list of N-lists using itertools
nested_list = list(itertools.repeat([], n))

# Displaying the nested list
print("Initial:", nested_list)

# Adding an element to demonstrate shared reference issue
nested_list[0].append("test")
print("After modifying one sublist:", nested_list)
Initial: [[], [], []]
After modifying one sublist: [['test'], ['test'], ['test']]

Creating Pre-populated Lists

You can also create lists with initial values using list comprehension ?

n = 3  # Number of sublists
m = 4  # Number of elements in each sublist

# Create lists with initial values
nested_list = [[0] * m for _ in range(n)]
print("With zeros:", nested_list)

# Create lists with different initial values
nested_list = [[i] * 3 for i in range(n)]
print("With different values:", nested_list)
With zeros: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
With different values: [[0, 0, 0], [1, 1, 1], [2, 2, 2]]

Comparison

Method Independent Sublists? Readability Best For
Simple Multiplication No (shared references) Poor Avoid this approach
Loop Yes Good Complex initialization logic
List Comprehension Yes Excellent Most cases (recommended)
itertools.repeat() No (shared references) Poor Avoid this approach

Conclusion

Use list comprehension [[] for _ in range(n)] for creating independent sublists. Avoid simple multiplication and itertools.repeat() as they create shared references that cause unexpected behavior when modifying sublists.

Updated on: 2026-03-27T11:14:51+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements