Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Flatten tuple of List to tuple - Python
Next article icon

Flatten tuple of List to tuple - Python

Last Updated : 18 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3).

Using itertools.chain()

This method is a highly efficient to flatten a tuple of lists into a tuple. chain.from_iterable() combines multiple inner lists into a single sequence without creating intermediate lists. It is particularly suitable for large datasets, as it lazily iterates over elements, reducing memory consumption.

Python
from itertools import chain  tup = ([5, 6], [6, 7, 8, 9], [3])  res = tuple(chain.from_iterable(tup)) print(res) 

Output
(5, 6, 6, 7, 8, 9, 3) 

Explanation: chain.from_iterable(tup) iterates over each sublist inside tup and extracts elements from each sublist in sequence, producing a single continuous stream of elements and tuple() converts this flattened stream into a tuple, creating the final flattened tuple.

Table of Content

  • Using list comprehension
  • Using sum()
  • Using functools.reduce()

Using list comprehension

List comprehension offers a concise approach to flatten a tuple of lists into a single tuple. It unpacks each sublist within the tuple and extracts individual elements into a flat tuple. Though slightly less efficient than itertools.chain() for large datasets, it is still fast and works well for smaller inputs.

Python
tup = ([5, 6], [6, 7, 8, 9], [3])  res = tuple(x for sublist in tup for x in sublist) print(res) 

Output
(5, 6, 6, 7, 8, 9, 3) 

Explanation: for sublist in tup iterates through each sublist in tup and for x in sublist iterates over each element x within them. The expression x extracts and flattens all elements into a single sequence and tuple() converts it into the final flattened tuple.

Using sum()

sum() can flatten a tuple of lists by summing the sublists , starting with an empty list. Each concatenation creates a new intermediate list, making it memory-intensive and slow for flattening tuples of lists.

Python
tup = ([5, 6], [6, 7, 8, 9], [3])  res = tuple(sum(tup, [])) print(res) 

Output
(5, 6, 6, 7, 8, 9, 3) 

Explanation: sum(tup, []) concatenates all sublists in tup into a single list, starting with an empty list [], and tuple() converts the result into a flattened tuple of elements.

Using functools.reduce()

reduce() repeatedly applies a function to combine elements of a tuple of lists into a single flattened list. This functional approach can flatten nested lists, but introduces performance overhead due to function call evaluations. It creates intermediate lists during reductions, making it slower than itertools.chain() and comprehension for flattening tuples of lists.

Python
from functools import reduce tup = ([5, 6], [6, 7, 8, 9], [3])  res = tuple(reduce(lambda x, y: x + y, tup)) print(res) 

Output
(5, 6, 6, 7, 8, 9, 3) 

Explanation: reduce(lambda x, y: x + y, tup) combines all sublists in tup by repeatedly concatenating pairs of sublists into a single list and tuple() converts the result into a flattened tuple of elements.


Next Article
Flatten tuple of List to tuple - Python

M

manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

    Python | Flatten Tuples List to String
    Sometimes, while working with data, we can have a problem in which we need to perform interconversion of data. In this, we can have a problem of converting tuples list to a single String. Let's discuss certain ways in which this task can be performed. Method #1: Using list comprehension + join() The
    7 min read
    Swap tuple elements in list of tuples - Python
    The task of swapping tuple elements in a list of tuples in Python involves exchanging the positions of elements within each tuple while maintaining the list structure. Given a list of tuples, the goal is to swap the first and second elements in every tuple. For example, with a = [(3, 4), (6, 5), (7,
    3 min read
    Python | Reverse each tuple in a list of tuples
    Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Nega
    5 min read
    Python - Convert List of Lists to Tuple of Tuples
    Sometimes, while working with Python data, we can have a problem in which we need to perform interconversion of data types. This kind of problem can occur in domains in which we need to get data in particular formats such as Machine Learning. Let us discuss certain ways in which this task can be per
    8 min read
    Sort Tuple of Lists in Python
    The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences