• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Flatten List of Tuples in Python

Flatten List of Tuples in Python

July 14, 2022 Leave a Comment

To flatten a list of tuples in Python, the easiest way is to use list comprehension.

list_of_tuples = [(0, 1), (2, 3), (4, 5)]

flattened_list = [x for tuple in list_of_tuples for x in tuple]

print(flattened_list)

#Output:
[0, 1, 2, 3, 4, 5]

You can also use the sum() function.

list_of_tuples = [(0, 1), (2, 3), (4, 5)]

flattened_list = list(sum(list_of_tuples,()))

print(flattened_list)

#Output:
[0, 1, 2, 3, 4, 5]

When working with collections of data, the ability to easily modify the structure and create new structures can be useful.

One such situation is if you have a list of tuples and want to flatten the list of tuples to create a simple list.

To flatten a list of tuples in Python, the easiest way is to use list comprehension.

Below is an example which will flatten a list of tuples using list comprehension in Python.

list_of_tuples = [(0, 1), (2, 3), (4, 5)]

flattened_list = [x for tuple in list_of_tuples for x in tuple]

print(flattened_list)

#Output:
[0, 1, 2, 3, 4, 5]

Using sum() to Flatten List of Tuples in Python

Another method you can use to flatten a list of tuples is with the Python sum() function.

The key here is that you need to pass a second value which will be the starting point for the sum() function.

In this case, we want to pass an empty tuple to sum() so that we can build a new list from scratch.

Below is an example showing you how to use sum()list_of_tuples = [(0, 1), (2, 3), (4, 5)] flattened_list = list(sum(list_of_tuples,())) print(flattened_list) #Output: [0, 1, 2, 3, 4, 5]

Hopefully this article has been useful for you to be able to flatten a list of tuples in Python.

Other Articles You'll Also Like:

  • 1.  Sort Files by Date in Python
  • 2.  How to Return Nothing in Python from Function
  • 3.  pandas T Function – Transposing DataFrames with pandas
  • 4.  Replace Values in Dictionary in Python
  • 5.  Python turtle dot() – Draw Dot on Turtle Screen
  • 6.  Inverting Dictionary Variables in Python with Dictionary Comprehension
  • 7.  Find Least Common Multiple of Numbers Using Python
  • 8.  Remove First and Last Character from String Using Python
  • 9.  How to Check if Number is Power of 2 in Python
  • 10.  How to Multiply All Elements in List Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy