pythonone-liner

why am i getting this?? <generator object <genexpr> at 0x000001C468108DC8>


i was trying to convert some part of my code into a one-liner but I am getting something unexpected

print(x for x in [2,3,4,5] if x%2==0)

can anyone tell why am I getting this - <generator object <genexpr> at 0x000001C468108DC8> instead of 2 and 4??


Solution

  • You are printing a generator object currently but you can easily turn it into a list comprehension and then it will have the behavior you want:

    print([x for x in [2,3,4,5] if x%2==0])