4. Building lists


#1


https://www.codecademy.com/courses/python-beginner-en-KAgt5/1/1?curriculum_id=4f89dab3d788890003000096


Why would you ever use this:

evens_to_50 = [i for i in range(51) if i % 2 == 0]
print evens_to_50

As apposed to this:

evens_to_50 = range(0, 51, 2)
print evens_to_50


#2

Honestly it doesn't really matter which way you do it as long as you get the same result. Both options are readable enough though one is elongated. Obviously the longer way has more functionality and can use operators to make more complicated lists, and it can handle floats unlike range().


#3

Sometimes this:

is better than the other option. It really depends on the situation.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.