You might have read about list comprehension, dictionary comprehension, set comprehension, etc in Python. However, there is no tuple comprehension in Python. In this article, we will discuss why there is no tuple comprehension in Python. We will also discuss how we can emulate tuple comprehension in Python.
Why Don’t We Have Tuple Comprehension in Python?
Comprehensions are used in Python to create iterable objects from other iterable objects. For instance, we define a list comprehension using the square brackets [] to create lists from other iterable objects using the following syntax.
newList=[element for element in iterable if condition]
You can observe this in the following example.
oldList=[1,2,3,4,5,6]
print("The old list is:")
print(oldList)
newList=[element**2 for element in oldList]
print("The new list is:")
print(newList)
Output:
The old list is:
[1, 2, 3, 4, 5, 6]
The new list is:
[1, 4, 9, 16, 25, 36]
Similarly, we define set comprehension using curly braces {} to create sets from other iterable using the following syntax.
newSet={element for element in iterable if condition}
You can observe this in the following example.
oldSet={1,2,3,4,5,6}
print("The old set is:")
print(oldSet)
newSet={element**2 for element in oldSet}
print("The new set is:")
print(newSet)
Output:
The old set is:
{1, 2, 3, 4, 5, 6}
The new set is:
{1, 4, 36, 9, 16, 25}
Now, you might think that we can define tuple comprehension using parentheses () to create tuples from other iterable objects using the following syntax.
newTuple= (element for element in iterable if condition)
Let’s try and create a tuple using the above syntax.
oldTuple=(1,2,3,4,5,6)
print("The old tuple is:")
print(oldTuple)
newTuple=(element**2 for element in oldTuple)
print("The new tuple is:")
print(newTuple)
Output:
The old tuple is:
(1, 2, 3, 4, 5, 6)
The new tuple is:
<generator object <genexpr> at 0x7fa988686f10>
In the output, you can observe that we get a generator object instead of a tuple. Thus, the syntax (element for element in iterable if condition) is used for generator comprehension and not tuple comprehension.
Hence, we don’t have tuple comprehension in Python as the parentheses are used for generator comprehension. Another reason for the absence of tuple comprehension might be that tuples are immutable.
Imitate Tuple Comprehension Using The Generator Comprehension
Although we cannot have tuple comprehension in Python, you can imitate it using different ways. For instance, you can use the following steps to imitate tuple comprehension using generator comprehension.
- First, you need to create a generator using generator comprehension to obtain the required elements in the output tuple.
- Then, you can use the tuple() function to obtain the output tuple. The tuple() function takes the generator as its input argument and returns a tuple.
You can observe this in the following example.
oldTuple=(1,2,3,4,5,6)
print("The old tuple is:")
print(oldTuple)
newTuple=tuple((element**2 for element in oldTuple))
print("The new tuple is:")
print(newTuple)
Output:
The old tuple is:
(1, 2, 3, 4, 5, 6)
The new tuple is:
(1, 4, 9, 16, 25, 36)
In the above output, you can observe that we have obtained a tuple using generator comprehension and the tuple() function.
Instead of the tuple() function, you can also use the unpacking operator to imitate tuple comprehension using the generator. For this, you just need to unpack the generator created from generator comprehension using the * operator. Then, you can place a comma “,” at the end of the unpacked elements to create a tuple as shown below.
oldTuple=(1,2,3,4,5,6)
print("The old tuple is:")
print(oldTuple)
newTuple=*(element**2 for element in oldTuple),
print("The new tuple is:")
print(newTuple)
Output:
The old tuple is:
(1, 2, 3, 4, 5, 6)
The new tuple is:
(1, 4, 9, 16, 25, 36)
In the above syntax, don’t forget the comma “,” at the end of the generator after unpacking it.
Imitate Tuple Comprehension Using List Comprehension
Just like generator comprehension, you can also use list comprehension and the tuple() function to imitate tuple comprehension as shown below.
oldTuple=(1,2,3,4,5,6)
print("The old tuple is:")
print(oldTuple)
newTuple=tuple([element**2 for element in oldTuple])
print("The new tuple is:")
print(newTuple)
Output:
The old tuple is:
(1, 2, 3, 4, 5, 6)
The new tuple is:
(1, 4, 9, 16, 25, 36)
You can also use the unpacking operator with list comprehension to imitate tuple comprehension in Python as shown below.
oldTuple=(1,2,3,4,5,6)
print("The old tuple is:")
print(oldTuple)
newTuple=*[element**2 for element in oldTuple],
print("The new tuple is:")
print(newTuple)
Output:
The old tuple is:
(1, 2, 3, 4, 5, 6)
The new tuple is:
(1, 4, 9, 16, 25, 36)
Conclusion
In this article, we discussed why there is no tuple comprehension in Python. We also discussed how we can imitate tuple comprehension using list and generator comprehension. Here, we have discussed four ways to imitate tuple comprehension. Out of these methods, the approach using the tuple() function and generator comprehension is the fastest whereas the approach using the list comprehension and unpacking operator is the slowest.
To learn more about Python programming, you can read this article on dictionary comprehension in Python. You might also like this article on Python continue vs break statements.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.