Shady Xu Shady Xu - 1 year ago 124
Python Question

Why is there no tuple comprehension in Python?

As we all know, there's list comprehension, like

[i for i in [1, 2, 3, 4]]

and there is dictionary comprehension, like

{i:j for i, j in {1: 'a', 2: 'b'}.items()}


(i for i in (1, 2, 3))

will end up in a generator, not a
comprehension. Why is that?

My guess is that a
is immutable, but this does not seem to be the answer.

Answer Source

You can use a generator expression:

tuple(i for i in (1, 2, 3))

but parenthesis were already taken for.. generator expressions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download