Python - Create nested list containing values as the count of list items
Last Updated :
06 Feb, 2023
Given a list, the task is to write a Python program to create a nested list where the values are the count of list items.
Examples:
Input: [1, 2, 3]
Output: [[1], [2, 2], [3, 3, 3]]
Input: [4, 5]
Output: [[1, 1, 1, 1], [2, 2, 2, 2, 2]]
Method 1: Using nested list comprehension
The list will contain the count of the list items for each element e in the list we will create a list in the list with size e, in each list we will append the element e for e times.
Python3
l = [1, 2, 3, 4, 5]
l = [[i+1 for j in range(l[i])] for i in range(len(l))]
print(l)
Output:
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
Time Complexity : O(N^2)
Space Complexity : O(N^2)
Method 2:
The list will contain the count of the list items iterate the for loop for L times i.e. length of the list. Now at each element append a list, The appended list will be of the size count.
Python3
l = [1, 2, 3, 4, 5]
for i in range(len(l)):
l[i] = [i+1 for j in range(i+1)]
print(l)
Output:
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
Time Complexity : O(N^2)
Space Complexity : O(N)
Method 3: Using *
Python3
l = [1, 2, 3, 4, 5]
nl=[]
for i in range(0,len(l)):
j=[i+1]
nl.append(j*l[i])
print(nl)
Output[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
Time Complexity : O(N^2)
Space Complexity : O(N)
Similar Reads
Python | Convert a nested list into a flat list In this article, we will cover how to Flatten a List of Lists in python. To convert a nested list into a flat list we are going to see some examples. Example: Input : l = [1, 2, [3, 4, [5, 6] ], 7, 8, [9, [10] ] ] Output : l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Input : l = [[['item1', 'item2']], [['ite
5 min read
Count the Sublists Containing given Element in a List - Python Given a list of lists our task is to count the number of sublists containing the given element x. For example: li = [[1, 3, 5], [1, 3, 5, 7], [1, 3, 5, 7, 9]] and x = 1 then output will be 3.Using List ComprehensionThis method uses list comprehension to check for the presence of the given element in
2 min read
Convert a List of Lists into Tree-like Dict - Python We are given a list of lists we need to convert it into tree-like dictionary. For example we are given a list of list li = [["a", "b", "c"], ["a", "b", "d"], ["x", "y"]] we need to convert it into tree like dict so that the output should be like {'a': {'b': {'c': {}, 'd': {}}}, 'x': {'y': {}}} .We c
5 min read
Counting number of unique values in a Python list Counting the number of unique values in a Python list involves determining how many distinct elements are present disregarding duplicates.Using a SetUsing a set to count the number of unique values in a Python list leverages the property of sets where each element is stored only once.Pythonli = [1,
2 min read
Counting the Frequencies in a List Using Dictionary in Python Counting the frequencies of items in a list using a dictionary in Python can be done in several ways. For beginners, manually using a basic dictionary is a good starting point. Using Manual MethodIt uses a basic dictionary to store the counts, and the logic checks whether the item is already in the
3 min read