
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Nested list comprehension in python
A nested list is a list within a list. Python provides features to handle nested list gracefully and apply common functions to manipulate the nested lists. In this article we will see how to use list comprehension to create and use nested lists in python.
Creating a Matrix
Creating a matrix involves creating series of rows and columns. We can use for loop for creating the matrix rows and columns by putting one python list with for loop inside another python list with for loop.
Example
matrix = [[m for m in range(4)] for n in range(3)] print(matrix)
Running the above code gives us the following result:
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
Filtering from Nested List
We can use the list comprehension with filtering feature by using the for loop within the sub-lists. Below we have a 2 dimensional list with one layer of sub-list inside a bigger list. We access selective elements from each of these nested lists. By using a filter condition.
Example
years = [['January', 'February', 'March'], ['April', 'May', 'June'], ['July', 'August', 'September'],['October','November','December']] # Nested List comprehension with an if condition years = [years for sublist in years for years in sublist if len(years) <= 5] print(years)
Running the above code gives us the following result:
['March', 'April', 'May', 'June', 'July']
Flattening nested Sub-Lists
We can also take a nested list and flatten it by creating a single list without having any sub-list inside it.
Example
nested_list = [[1] ,[17, 19], [21,23,25], [32,35,37,39]] # Nested List Comprehension to flatten a given 2-D matrix flattened_list = [value for sublist in nested_list for value in sublist] print(flattened_list)
Running the above code gives us the following result:
[1, 17, 19, 21, 23, 25, 32, 35, 37, 39]
- Related Articles
- Python List Comprehension?
- Python List Comprehension and Slicing?
- Count set bits using Python List comprehension
- How to create a dictionary with list comprehension in Python?
- How to catch a python exception in a list comprehension?
- Flatten Nested List Iterator in Python
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- How will you explain Python for-loop to list comprehension?
- Python - Convert given list into nested list
- Python Dictionary Comprehension
- Nested List Weight Sum II in Python
- Python - Convert List to custom overlapping nested list
- Convert a nested list into a flat list in Python
- Move all zeroes to end of the array using List Comprehension in Python
- List comprehension and ord() in Python to remove all characters other than alphabets
