May-07-2018, 01:47 PM
In that form is just to create a list, as the [x for x in xxx] is a syntactic sugar for list(x for x in xxx).
In particular the part of "x for x in xxx" produces a generator, that you can pass it to any place in python that accepts a generator as input (behaves as the command "range")
The other 2 "pretty" syntax similar to that one is to create a set or a dictionary:
In particular the part of "x for x in xxx" produces a generator, that you can pass it to any place in python that accepts a generator as input (behaves as the command "range")
The other 2 "pretty" syntax similar to that one is to create a set or a dictionary:
# Imagine fd contains 1, 2, 555, 44 # You only want the different numbers, no matter the repetitions => Create set of numbers >>> {int(s.strip()) for s in fd} {1, 2, 555, 44} # You want to create a dictionary initialising the entry to something >>> {int(s.strip()): 'xxx' for s in fd} {1: 'xxx', 2: 'xxx', 44: 'xxx', 555: 'xxx'} # If you want to create a tuple you can do it with: >>> tuple(int(s.strip()) for s in fd) (1, 2, 44, 555)