I'm trying to use walrus in a for loop to create a list,
something like this:
data = [l := line.strip().somefunc() for line in iterable_obj if(l[0] == 'sth')]
but it returns an empty list can someone give me a hint on what I'm doing wrong here?
I'm trying to use walrus in a for loop to create a list,
something like this:
data = [l := line.strip().somefunc() for line in iterable_obj if(l[0] == 'sth')]
but it returns an empty list can someone give me a hint on what I'm doing wrong here?
On
No need to use walrus here, just do:
iterable_obj = [' Hello world sth', ' here', 'we', 'go']
data = [line.strip() for line in iterable_obj if ('sth' not in line)]
print(data)
Output:
['here', 'we', 'go']
You just need to swap where you put the assignment expression (because the condition is evaluated first):