Flatten List in Python with Example

Flattening a list refers to the process of removing a dimension from a list. A dimension refers to an additional co-ordinate needed to locate an item in a list. You can flatten a Python list using a list comprehension, a nested for loop, or the itertools.chain() method.

Flatten List using Nested for Loop

Using the nested for loop to append each element of sublist in a flat list


nestedlist = [ [1, 2, 3, 4], ["Ten", "Twenty", "Thirty"], [1.1,  1.0E1, 1+2j, 20.55, 3.142]]
for sublist in nestedlist:
    for element in sublist:


[1, 2, 3, 4, 'Ten', 'Twenty', 'Thirty', 1.1, 10.0, (1+2j), 20.55, 3.142]

Using List Comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of the existing list. A list comprehension consists of brackets containing the expression, which is executed for each element, and the for loop to iterate over each element.

original_list = [[11, 21, 30], [19, 63, 71], [81, 99]]

flatten_list = [element for sublist in original_list for element in sublist]

print("Original list", original_list)

print("Flattened list", flatten_list)


Original list [[11, 21, 30], [19, 63, 71], [81, 99]]

Flattened list [11, 21, 30, 19, 63, 71, 81, 99]

You can see that we have flattened a list. 

List comprehensions provide a concise way to create lists.

Flatten List of Lists Using itertools

Using itertools is ideal for transforming a 2D list into a single flat list. It treats consecutive sequences as a single sequence by iterating through the iterable passed as the argument sequentially.

import itertools

original_list = [[11, 21, 30], [19, 63, 71], [81, 99]]

flatten_list = list(itertools.chain(*original_list))

print("Original list", original_list)

print("Flattened list", flatten_list)


Original list [[11, 21, 30], [19, 63, 71], [81, 99]]

Flattened list [11, 21, 30, 19, 63, 71, 81, 99]

And we got the flatten list in the output.

While itertools is an effective way at flattening a list, it is more advanced than the last approach we have discussed.

This is because you must import itertools into your code which introduces a new dependency. What’s more, the chain() method involves unpacking which can be difficult to understand.

Flatten List of Lists Using numpy (concatenate() and flat())

To flatten a list of lists in Python, use the combination of numpy library, concatenate(), and flat() function. Numpy offers common operations, which include concatenating regular 2D arrays row-wise or column-wise. We are also using the flat attribute to get a 1D iterator over the array to achieve our goal.

import numpy as np

original_list = [[11, 21, 30], [19, 63, 71], [81, 99]]

flatten_list = list(np.concatenate(original_list). flat)

print("Original list", original_list)

print("Flattened list", flatten_list)


Original list [[11, 21, 30], [19, 63, 71], [81, 99]]

Flattened list [11, 21, 30, 19, 63, 71, 81, 99]


What is GEEK

Buddha Community

Ray  Patel

Ray Patel


top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel


Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Python Tips and Tricks for Competitive Programming

Python Programming language makes everything easier and straightforward. Effective use of its built-in libraries can save a lot of time and help with faster submissions while doing Competitive Programming. Below are few such useful tricks that every Pythonist should have at their fingertips:

  • **Converting a number into a List of digits using map() Function: **

Below is the implementation to convert a given number into a list of digits:

#competitive programming #python programs #python-itertools #python-library #python-list #python-list-of-lists #python-map

Osiki  Douglas

Osiki Douglas


The anatomy of Python Lists

An easy guide to summarize the most common methods and operations regarding list manipulation in Python.

Python lists are a built-in type of data used to store items of any data type such as strings, integers, booleans, or any sort of objects, into a single variable.

Lists are created by enclosing one or multiple arbitrary comma-separated objects between square brackets.

Lists may contain elements of different data types

List items follows a sequenced or specific order

Access values by index

#python-programming #python #tutorial #list-manipulation #python-list #the anatomy of python lists

Arvel  Parker

Arvel Parker


Basic Data Types in Python | Python Web Development For Beginners

At the end of 2019, Python is one of the fastest-growing programming languages. More than 10% of developers have opted for Python development.

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Table of Contents  hide

I Mutable objects

II Immutable objects

III Built-in data types in Python

Mutable objects

The Size and declared value and its sequence of the object can able to be modified called mutable objects.

Mutable Data Types are list, dict, set, byte array

Immutable objects

The Size and declared value and its sequence of the object can able to be modified.

Immutable data types are int, float, complex, String, tuples, bytes, and frozen sets.

id() and type() is used to know the Identity and data type of the object







Built-in data types in Python

a**=str(“Hello python world”)****#str**














Numbers (int,Float,Complex)

Numbers are stored in numeric Types. when a number is assigned to a variable, Python creates Number objects.

#signed interger




Python supports 3 types of numeric data.

int (signed integers like 20, 2, 225, etc.)

float (float is used to store floating-point numbers like 9.8, 3.1444, 89.52, etc.)

complex (complex numbers like 8.94j, 4.0 + 7.3j, etc.)

A complex number contains an ordered pair, i.e., a + ib where a and b denote the real and imaginary parts respectively).


The string can be represented as the sequence of characters in the quotation marks. In python, to define strings we can use single, double, or triple quotes.

# String Handling

‘Hello Python’

#single (') Quoted String

“Hello Python”

# Double (") Quoted String

“”“Hello Python”“”

‘’‘Hello Python’‘’

# triple (‘’') (“”") Quoted String

In python, string handling is a straightforward task, and python provides various built-in functions and operators for representing strings.

The operator “+” is used to concatenate strings and “*” is used to repeat the string.


output**:****‘Hello python’**

"python "*****2

'Output : Python python ’

#python web development #data types in python #list of all python data types #python data types #python datatypes #python types #python variable type