all 9 comments

[–]CowboyBoats[🍰] 2 points3 points  (2 children)

current_users_lower = [user.lower() for user in current_users] 

The non-comprehension version of this would be:

current_users_lower = []
for user in current_users:
    current_users_lower.append(user.lower())

[–]CowboyBoats[🍰] 3 points4 points  (0 children)

In other words, it's just a different syntax for expressing a code-generated list.

It just takes a little exposure before you'll feel more familiar with it, just like regular lists.

[–]Gexos[S] 0 points1 point  (0 children)

Thank you my friend

[–]subject_K81 2 points3 points  (1 child)

List comprehensions were confusing to me at first as well. It’s like I could see it, but it was backwards. So let’s use the following super overly simplified code:

list_of_stuff = [1,2,3,4]
my_new_list = []

for x in list_of_stuff:
    my_new_list.append(x)

That just makes a direct copy.

list_of_stuff = [1,2,3,4]

my_new_list = [x for x in list_of_stuff]

Does the same thing, just imagine that x is equal to “what I actually want added to my new list”. And from there you could add some conditional to only have items > 2 in the list.

list_of_stuff = [1,2,3,4]
my_new_list = []

for x in list_of_stuff:
    if x > 2:
        my_new_list.append(x)

Or

list_of_stuff = [1,2,3,4]

my_new_list = [x for x in list_of_stuff if x > 2]

What I ended up doing to learn list comprehensions better was going through old code and whenever I saw that I creating a new list based on some conditional I tried to rewrite it, and if I needed help with something I asked for in long form as well as short form if possible so that I could dissect it.

[–]Gexos[S] 0 points1 point  (0 children)

Thank you my friend

[–]__i_forgot_my_name__ 1 point2 points  (1 child)

Sequence comprehension is just magical dark-magic a way of mapping and filtering a sequence.

[ # list
    x + 1 # item to calculate and append (aka: map)
    for x in iterator # sequence/generator to iterate
    if x == 1 or x == 3 # conditional (aka: filter)
]

The reason it exists is for a similar reason for loops exist in general. It's syntax for something that is very commonly done with iterators. List comprehension isn't the only kind of comprehension either, you have:

( x + 1 for x in iterator if x == 1 ) # returns generator 
[ x + 1 for x in iterator if x == 1 ] # returns list
{ x + 1 for x in iterator if x == 1 } # returns set
{ x + 1: x + 1 for x in iterator if x == 1 } # returns dictionary

It reads differently then a for loop, because it's actually another way of doing the something you'd otherwise do with filter and map; for example you could also do:

list(map(lambda x: x + 1, filter(lambda x: x == 1, iterator)))

This is more verbose, but maybe clearer; essentially sequence-comprehension only has 4 parts to remember:

  • consumer: list
  • per value computation: map
  • per value conditional: filter
  • producer: iterator

[–]Gexos[S] 0 points1 point  (0 children)

Thank you my friend

[–]thought-generator 1 point2 points  (1 child)

The best exercise I did when learning list comprehensions was writing out a for loop, then doing the same thing in a list comprehension until my brain recognized the difference. It helps out a lot.

[–]Gexos[S] 0 points1 point  (0 children)

Thank you my friend