Python allows you to create lists of tuples where each item of list is a tuple. Sometimes you may need to flatten a list of tuples in python to obtain a single list of items, and use it further in your application. In this article, we will look at how to flatten list of tuples.
There are many ways to flatten list of tuples in Python. Let us say you have the following list of tuples.
>>> a=[[1,2],[3,4],[5,6]]
1. Using sum
This is the easiest and fastest method to convert a list of tuples. We use sum function to add an empty tuple to our list of tuples. The result is a single tuple of all elements. We convert this tuple into list using list function. Please note, list function is available only in python 3+.
>>> sum[a,[]] [1, 2, 3, 4, 5, 6] >>> list[sum[a,[]]] [1, 2, 3, 4, 5, 6]
# one line command >>> b=list[sum[a,[]]] >>> b [1, 2, 3, 4, 5, 6]
2. Using itertools
itertools is a useful library that allows you to easily work with iterable data structure like lists. It provides chain function that allows you to easily flatten a list. Here is an example.
>>> import itertools >>> list[itertools.chain[*a]] [1, 2, 3, 4, 5, 6] >>> list[itertools.chain.from_iterable[a]] [1, 2, 3, 4, 5, 6]
3. Using List Comprehension
You an also use list comprehensions to flatten a list of tuples as shown below. In this case, we basically loop through our list of tuples to construct our flattened list.
>>> b = [item for sublist in a for item in sublist]
[1, 2, 3, 4, 5, 6]
4. Using extend
You may also use extend method to flatten list of tuple. But please note, this method is slower than others and supported only in Python 3+.
>>> b= []
>>>list[b.extend[item] for item in a]
>>> b [1, 2, 3, 4, 5, 6]
In this article, we have looked at various methods to flatten list of tuples. Out of them, the first method is the fastest, simplest and most recommended. However, it uses list[] function which is available in python 3+. If you use python >> 7.022608777000187 [l.extend[row] for row in output] >>> 9.155910597999991 [item for sublist in output for item in sublist] >>> 9.920002304000036 list[l.extend[row] for row in output] >>> 10.703829122000116
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have lists as its constituent elements. This kind of problem is common in data domains such as Machine Learning. Let’s discuss certain ways in which this task can be performed.
Input : test_tuple = [[5], [6], [3], [8]] Output : [5, 6, 3, 8] Input : test_tuple = [[5, 7, 8]] Output : [5, 7, 8]
Method #1 : Using sum[] + tuple[] The combination of above functions can be used to solve this problem. In this, we perform the task of flattening using sum[], passing empty list as its argument.
Python3
test_tuple
=
[[
5
,
6
], [
6
,
7
,
8
,
9
], [
3
]]
print
["The original
tuple
: "
+
str
[test_tuple]]
res
=
tuple
[
sum
[test_tuple, []]]
print
["The flattened
tuple
: "
+
str
[res]]
Output :
The original tuple : [[5, 6], [6, 7, 8, 9], [3]] The flattened tuple : [5, 6, 6, 7, 8, 9, 3]
Method #2 : Using tuple[] + chain.from_iterable[] The combination of above functions can be used to solve this problem. In this, we perform task of flattening using from_iterable[] and conversion to tuple using tuple[].
Python3
from
itertools
import
chain
test_tuple
=
[[
5
,
6
], [
6
,
7
,
8
,
9
], [
3
]]
print
["The original
tuple
: "
+
str
[test_tuple]]
res
=
tuple
[chain.from_iterable[test_tuple]]
print
["The flattened
tuple
: "
+
str
[res]]
Output :
The original tuple : [[5, 6], [6, 7, 8, 9], [3]] The flattened tuple : [5, 6, 6, 7, 8, 9, 3]
Method #3: Using extend[] and tuple[] methods
Python3
test_tuple
=
[[
5
,
6
], [
6
,
7
,
8
,
9
], [
3
]]
print
[
"The original tuple : "
+
str
[test_tuple]]
res
=
[]
for
i
in
test_tuple:
res.extend[i]
res
=
tuple
[res]
print
[
"The flattened tuple : "
+
str
[res]]
Output :
The original tuple : [[5, 6], [6, 7, 8, 9], [3]]
The flattened tuple : [5, 6, 6, 7, 8, 9, 3]