How to flatten a nested list of tuples into a single list of tuples in Python

1 Answer

0 votes
# Create a nested list of tuples
data = [
    [(1, 2), (3, 4)],
    [(5, 6), (7, 8)],
    [(9, 10), (11, 12)]
]

# Print the entire structure
print("Nested list of tuples:", data)

# Flattening into a single list of tuples
flat_list = [tup for sublist in data for tup in sublist]
print("\nFlattened list:", flat_list)



'''
run:

Nested list of tuples: [[(1, 2), (3, 4)], [(5, 6), (7, 8)], [(9, 10), (11, 12)]]

Flattened list: [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12)]

'''

 



answered Feb 9 by avibootz

Related questions

2 answers 205 views
...