#๐Ÿ”’ understanding the syntax of list comprehensions

63 messages ยท Page 1 of 1 (latest)

crisp herald
#

I need help understanding list comprehensions in python.
per w3 the syntax for a list comprehension is:

newlist = [expression for item in iterable if condition == True]

after watching youtube videos I see people inputting what they want for the expression part of the syntax.

and than we use the for loop to feed the numbers into the expression.

So basically we create an expression or a formula that will apply the calculation for the for loop first, and than write the actual for loop. is this correct?

is there a simpler way to explain the syntax?

rose gazelleBOT
#

@crisp herald

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

crude pendant
crisp herald
#

reviewing

#

color coded!

#

okay so the blue is the variable holding the data

#

red seems to be the action that is taken on the data

#

green is the for loop itself

crude pendant
#

not necessarily

crisp herald
#

yello is the operation in the for loop?

crude pendant
#

in for item in cart, item represents any given element of cart

#

in a list comprehension, [item for item in cart] means "a list with every element in cart"

crisp herald
#

thinking

crude pendant
#

the first term, before the "for" syntax. is added to the list "for every thing in an iterable"

#

so if a list has 4 things, and i did [ 10 for i in list_with_4_things ]

crisp herald
#

okay so add this to the list when the condition is true?

crude pendant
#

i would have a list which looks like [10, 10, 10, 10]

crisp herald
#

reviewing

#

10 for i in list with 4 things -> so it iterates 4 times and there are 4 things so it looks at the code before the for loop and will output that code up to the number of times the condition is met

mental owl
#

comprehensions actually combine the functionality of map and filter
if you know what those are then it might be clearer now
if you don't then just don't worry about this for now

crude pendant
crude pendant
#
x = "anything you can put here"

["you can also put here" for i in range(3)]
crisp herald
#

does the for loop feed values into the expression?

crude pendant
#

it can

crisp herald
#

can i see a simple example

mental owl
#

!e

l = [i+1 for i in range(5)]
print(l)
crisp herald
#

err you gave me one

rose gazelleBOT
#

@mental owl :white_check_mark: Your 3.12 eval job has completed with return code 0.

[1, 2, 3, 4, 5]
crisp herald
#

okay i see the list for 5 numbers using the range function

#

so basically

#

i gets added by one

#

and uh

#

the for loop creates the range of numbers

#

1,2,3,4,5

#

so would those numbers

#

be added

#

and it be

#

2,3,4,5,6

crude pendant
#
lst = [1, 2, 3, 4]
new_lst = [ i for i in lst if i % 2 == 0 ]

# "i" is created by the "for i in lst", its the iterator variable for the for loop and represents the elements of "lst"

# the list is then constructed of all "i" in "lst" such that i % 2 == 0
crude pendant
crisp herald
#

oh

#

okay so in the prior example

#

it would have been 0,1,2,3,4

#

and than the plus one would happen

#

so the output is

#

1,2,3,4,5

#

i see

crude pendant
#

if you cant understand the second one, i would learn the basics before shortcuts like this

crisp herald
#

but the expression seems to be the operation or equation that runs when the for loop will use after it has run its own calculations and the results of those calculations will generate the final result or new list

crisp herald
#

so the output would be 2,4

crude pendant
#

yes

crisp herald
#

@mental owl @crude pendant thank you, you are both effective teachers

crude pendant
#

fewer lines of code is not always better, readability is always the main concern

#

sometimes more efficient algorithms are actually more lines

crisp herald
#

!solved

rose gazelleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.