-4

So far, I've learned about

  • list
  • set
  • dictionary
  • generator

comprehensions. Are there any other iterables that can be ''comprehended''? I'm mostly interested in Python 3.

Nibor
  • 1,236
  • 9
  • 23
  • Note, however, that a generator is not a comprehension, despite the similarity in syntax. – Robᵩ Jun 01 '17 at 21:27
  • I know that, but you can write a generator as a comprehension, cf. https://nedbatchelder.com/blog/201605/generator_comprehensions.html – Nibor Jun 01 '17 at 21:39

1 Answers1

0
my_dict = {i:char for i,char in enumerate("Hello, world!")}
my_list = [i**2 for i in range(10)]
my_set = {i**2 for i in range(10)}
my_generator = (i**2 for i in range(10))

In terms of just comprehensions, there aren't any more that I'm aware of. You could of course use a list/generator comprehension within (say) a tuple constructor to create a (in this case) tuple. But that's not a generator per se

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • I know how these work. My question is if there are other comprehensions than the ones mentioned. – Nibor Jun 01 '17 at 21:18