Open In App

How to Create a List of N-Lists in Python

Last Updated : 28 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover how to create a list of n-lists in Python.

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

Example 1: Using simple multiplication 

In this example, we are multiplying the list by 4 to get a list of lists.

Python3




# Create a list with 4 references of same sub list
lis = [[]] * 4
print(lis)


Output:

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

Example 2: Using List comprehension 

In this example, we are using list comprehension for generating a list of lists.

Python3




d1 = [[] for x in range(6)]
print(d1)


Output:

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

Example 3: 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.

Python3




list1 = []
list2 = []
 
for x in range(0,5):
  list1.append(list2)
 
print(list1)


Output:

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

Example4 : 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:

Python3




from itertools import repeat
 
# Create a list of lists with 5 sub-lists
n_lists = list(repeat([], 5))
 
print(n_lists)  # Output: [[], [], [], [], []]


Output

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


Similar Reads

Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
How to Zip two lists of lists in Python?
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let's discuss certain ways i
7 min read
Python - Convert Lists into Similar key value lists
Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list. Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9] Output : {5: [8], 6: [3, 2, 9]} Explanation : Elements with index 6 in corresponding list, are mapped to 6. Input : test_list1
12 min read
Indexing Lists Of Lists In Python
Lists of lists are a common data structure in Python, providing a versatile way to organize and manipulate data. When working with nested lists, it's crucial to understand how to index and access elements efficiently. In this article, we will explore three methods to index lists of lists in Python using the creation of a sample list, followed by ex
3 min read
Python | Maximum sum of elements of list in a list of lists
Given lists in a list, find the maximum sum of elements of list in a list of lists. Examples: Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]] Output : 33 Explanation: sum of all lists in the given list of lists are: list1 = 6, list2 = 15, list3 = 33, list4 = 24 so the maximum among these is of Input : [[3, 4, 5], [1, 2, 3], [0, 9, 0]] Outpu
4 min read
Python | Ways to sum list of lists and return sum list
The list is an important container and is used almost in every code of day-day programming as well as web development, more it is used, the more is the requirement to master it and hence knowledge of its operations is necessary. Given a list of lists, the program to suppose to return the sum as the final list. Let's see some of the methods to sum a
5 min read
Python | Check if a list exists in given list of lists
Given a list of lists, the task is to check if a list exists in given list of lists. Input : lst = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]] list_search = [4, 5, 6] Output: True Input : lst = [[5, 6, 7], [12, 54, 9], [1, 2, 3]] list_search = [4, 12, 54] Output: False Let’s discuss certain ways in which this task is performed. Method #1: Using
4 min read
Python | Convert list into list of lists
Given a list of strings, write a Python program to convert each element of the given list into a sublist. Thus, converting the whole list into a list of lists. Examples: Input : ['alice', 'bob', 'cara'] Output : [['alice'], ['bob'], ['cara']] Input : [101, 202, 303, 404, 505] Output : [[101], [202], [303], [404], [505]] Approach #1 : Naive Approach
5 min read
Python | Sorting list of lists with similar list elements
Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Let's discuss certain ways in which this can be perf
5 min read
Python | Merge List with common elements in a List of Lists
Given a list of list, we have to merge all sub-list having common elements. These type of problems are very frequent in College examinations and while solving coding competitions. Below are some ways to achieve this. Input: [[11, 27, 13], [11, 27, 55], [22, 0, 43], [22, 0, 96], [13, 27, 11], [13, 27, 55], [43, 0, 22], [43, 0, 96], [55, 27, 11]] Out
3 min read
Article Tags :
Practice Tags :