1
x=(y for y in range(100) if y%2==0 and y%5==0)
print(x)

This is giving output like -

<generator object <genexpr> at 0x00000222B1C3CC80>
Niranjan
  • 29
  • 3
  • 2
    There is no such thing as a `tuple comprehension` in python. What you have is what it is telling you it is: a `generator` object. You can iterate over this if you want. – quamrana Jul 12 '21 at 10:25
  • A "tuple comprehension" would be this to turn this generator into a tuple through the `tuple` constructor: `tuple(y for y in ...)`. – deceze Jul 12 '21 at 10:26

3 Answers3

2

Here is what you can do

x=tuple(y for y in range(100) if y%2==0 and y%5==0)

2

Tuple comprehension is not a thing. Check this SO post to understand why.

Do this instead:

x = tuple(y for y in range(100) if y%2==0 and y%5==0)

This uses the tuple() constructor to make a tuple from the generator object. You can use this constructor to make a tuple from elements yielded by an iterator or iterable object. So, this code works because generator objects can be used as iterators.

Shubham
  • 1,310
  • 4
  • 13
1

You can use tuple() function to cast it.

x = (y for y in range(100) if y % 2 == 0 and y % 5 == 0)
print(tuple(x))
# (0, 10, 20, 30, 40, 50, 60, 70, 80, 90)
Ibrahim Berber
  • 842
  • 2
  • 16