For those that want to learn more about "one line filtering lists", they are an example of list comprehensions. In addition to filtering, you can use them to get a list of some property on objects, or perform some operation:
To add to this, the output of the range function is a generator which generates values on the fly as opposed to having them stored in memory all at once.
Furthermore, if you replace the square brackets of a list comprehension with curved brackets, you get a generator comprehension.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks! I don't think I knew about
sum
.For those that want to learn more about "one line filtering lists", they are an example of list comprehensions. In addition to filtering, you can use them to get a list of some property on objects, or perform some operation:
etc.
You can also make dictionary comprehensions.
A related concept is range:
Note that in Python 3, range is not a list but rather its own type. However, you can use it very similarly to a list.
To add to this, the output of the range function is a generator which generates values on the fly as opposed to having them stored in memory all at once.
Furthermore, if you replace the square brackets of a list comprehension with curved brackets, you get a generator comprehension.