- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Create nested list containing values as the count of list items
When it is required to create a nested list containing values as the count of list elements, a simple iteration is used.
Below is a demonstration of the same
my_list = [11, 25, 36, 24] print("The list is :") print(my_list) for element in range(len(my_list)): my_list[element] = [element+1 for j in range(element+1)] print("The resultant list is :") print(my_list)
The list is : [11, 25, 36, 24] The resultant list is : [, [2, 2], [3, 3, 3], [4, 4, 4, 4]]
A list is defined and is displayed on the console.
It is iterated over, and it is added to 1 and converted to a list.
This is converted to a list.
This is the output that is displayed on the console.
- Count the sublists containing given element in a list in Python
- How to access nested Python dictionary items via a list of keys?
- Python - Convert given list into nested list
- Python - Convert List to custom overlapping nested list
- Nested list comprehension in python
- How to count the number of items in a C# list?
- Program to count average of all special values for all permutations of a list of items in Python
- Python | Sort the values of first list using second list
- Python program to Flatten Nested List to Tuple List
- Flatten Nested List Iterator in Python
- Convert a nested list into a flat list in Python
- How to randomize the items of a list in Python?
- Program to update list items by their absolute values in Python
- Count number of items in a dictionary value that is a list in Python