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. This article will explore various methods to create a list of N−lists in Python, providing you with the knowledge and tools to efficiently manage and manipulate your data.

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.

Now let's explore the different approaches that we can make use of to create a list of N−lists in python.

Using Simple Multiplication

  • Define the dimensions of the list of lists. Let's say we want to create a list of size n with each sublist containing m elements.

  • Create a list comprehension where each element is a sublist of m empty elements.

  • Multiply this list comprehension by n to create a list of n sublists.

Consider the code shown below.

Example

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

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

# Displaying the nested list
print(nested_list)

Explanation

  • In this approach, we use a list comprehension to create a sublist of m empty elements.

  • By multiplying this sublist by n, we create a list of n sublists where each sublist is a reference to the same sublist created in the list comprehension.

  • This approach is efficient in terms of code simplicity, but it is important to note that modifying one sublist will affect other sublists since they refer to the same object.

Output

[[], [], [], [], [], [], [], [], []]

Using a Loop

  • Define the dimensions of the list of lists.

  • Create an empty list to store the sublists.

  • Use a loop to iterate n times.

  • Inside the loop, create a new empty sublist and append it to the main list.

Consider the code shown below.

Example

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

# 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(nested_list)

Explanation

  • In this approach, we use a loop to iterate n times, creating a new empty sublist in each iteration.

  • Each sublist is then appended to the main list, resulting in a list of n sublists.

  • This approach offers more flexibility and independence between the sublists compared to the simple multiplication approach.

Output

[[], [], []]

Using List Comprehension

  • Define the dimensions of the list of lists.

  • Create a list comprehension where each element is a new empty sublist.

  • Iterate n times to create n sublists.

Consider the code shown below.

Example

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

# 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(nested_list)

Explanation

  • This approach utilises a list comprehension to create n sublists, where each sublist is initially empty.

  • The list comprehension iterates n times and creates a new empty sublist in each iteration, resulting in a list of n sublists.

  • This approach provides a concise and efficient way to create the desired list of N-lists.

Output

[[], [], []]

Using itertools

  • Import the itertools module.

  • Define the dimensions of the list of lists.

  • Use the itertools.repeat() function to repeat an empty list n times.

  • Convert the repeated object to a list.

Consider the code shown below.

Example

import itertools

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

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

# Displaying the nested list
print(nested_list)

Explanation

  • In this approach, we import the itertools module to utilize the repeat() function.

  • The repeat() function is used to repeat an empty list n times.

  • We convert the repeated object to a list using the list() function, resulting in a list of n sublists.

  • This approach provides a concise way to create the desired list of N−lists using the itertools module.

Output

[[], [], []]

Conclusion

In conclusion, this article explored different approaches to create a list of N−lists in Python. By leveraging these approaches, you can efficiently organise and manage data in a structured manner.

Updated on: 04-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements