• 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
  • Ruby
  • 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.  Create Empty Tuple in Python
  • 2.  Python os.sep – Create Operating System Path Seperator Character
  • 3.  Using Python to Insert Tab in String
  • 4.  Write Float to File Using Python
  • 5.  Why Dictionaries Can’t Have Duplicate Keys in Python
  • 6.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 7.  Difference Between read(), readline() and readlines() in Python
  • 8.  Convert List into Tuple Using Python
  • 9.  Get Username in Python using os module
  • 10.  pandas product – Get Product of Series or DataFrame Columns

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy