
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How do make a flat list out of list of lists in Python?
A nested list is a list which has elements as lists itself.
For example: [[1,2,3], [4,5,6], [7,8,9]] is a nested list as it has 3 lists ([1,2,3], [4, 5, 6], and [7, 8, 9]) as its elements.
To flatten a list of lists (nested list) i.e., to convert a 2D list into a 1D list, there are different ways such as nested for loop, list comprehension, built-in functions or by importing libraries in python.
In this article we will discuss the above methods to flatten a list in Python.
Using for loops
For iterating repeatedly through a sequence, use a for loop (that is either a list, a tuple, a dictionary, a set, or a string). This functions more like an iterator method seen in other object-oriented programming languages and is less like the for keyword found in other programming languages.
Example
In the following example code, we must transform a nested list into a flat list which is a single 1D list. So, we have used the append() method to add elements into the list.
def flatlist(l): flat_list = [] for element in l: if type(element) is list: for item in element: flat_list.append(item) else: lat_list.append(element) return flat_list lst = [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]] print('Original List', lst) print('Flattend List', flatlist(lst))
Output
The following output is obtained on executing the above program.
Original List [[10, 20, 30, 40], [50, 60, 70], [80, 90, 100]] Flattend List [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Using List Comprehension
Python's list comprehension feature makes it simple to build new lists from pre-existing ones. It offers a more concise syntax for building new lists from of pre-existing ones and their values.
Example
In this example, the nested list is converted into a single 1D list using the list comprehension method.
original_list = [[10, 20, 30, 40], [50, 60, 70], [80, 90]] flat_list = [item for sublist in original_list for item in sublist] print('Original list', original_list) print('Flattend list', flat_list)
Output
The following output is obtained on executing the above program.
Original list [[10, 20, 30, 40], [50, 60, 70], [80, 90]] Flattend list [10, 20, 30, 40, 50, 60, 70, 80, 90]
Using NumPy library
A general-purpose package for handling arrays is called NumPy. It offers a multidimensional array object with outstanding speed as well as capabilities for interacting with these arrays.
Example
In the following example we used the NumPy library with other methods in it. Those method are mentioned below.
concatenate()function is used to concatenate a sequence of arrays.
flat() function is used as a 1D iterator over an N Dimensional array.
import numpy originallist = [[10, 20, 30, 40], [50, 60, 70], [80, 90]] flatlist = list(numpy.concatenate(originallist).flat) print('Original list', originallist) print('Transformed list', flatlist)
Output
The following output is obtained on executing the above program.
Original list [[10, 20, 30, 40], [50, 60, 70], [80, 90]] Transformed list [10, 20, 30, 40, 50, 60, 70, 80, 90]
- Related Articles
- Convert list into list of lists in Python
- Convert a nested list into a flat list in Python
- Python - Convert List of lists to List of Sets
- How to join list of lists in python?
- How do I get length of list of lists in Java?
- How to get length of a list of lists in Python?
- Check if a list exists in given list of lists in Python
- Convert a list into tuple of lists in Python
- Custom Multiplication in list of lists in Python
- Python - Column deletion from list of lists
- How to convert a list of lists into a single list in R?
- How do you make a shallow copy of a list in Java?
- How to append list to second list (concatenate lists) in Python?
- Find common elements in list of lists in Python
- How do you make a list iterator in Java?
