You are producing a filtered list by using a list comprehension. i is still being bound to each and every element of that list, and the last element is still 'three', even if it was subsequently filtered out from the list being produced.
You should not use a list comprehension to pick out one element. Just use a for loop, and break to end it:
for elem in my_list:
if elem == 'two':
break
If you must have a one-liner (which would be counter to Python's philosophy, where readability matters), use the next() function and a generator expression:
i = next((elem for elem in my_list if elem == 'two'), None)
which will set i to None if there is no such matching element.
The above is not that useful a filter; your are essentially testing if the value 'two' is in the list. You can use in for that:
elem = 'two' if 'two' in my_list else None
Answer from Martijn Pieters on Stack OverflowYou are producing a filtered list by using a list comprehension. i is still being bound to each and every element of that list, and the last element is still 'three', even if it was subsequently filtered out from the list being produced.
You should not use a list comprehension to pick out one element. Just use a for loop, and break to end it:
for elem in my_list:
if elem == 'two':
break
If you must have a one-liner (which would be counter to Python's philosophy, where readability matters), use the next() function and a generator expression:
i = next((elem for elem in my_list if elem == 'two'), None)
which will set i to None if there is no such matching element.
The above is not that useful a filter; your are essentially testing if the value 'two' is in the list. You can use in for that:
elem = 'two' if 'two' in my_list else None
When you perform
>>> [(i) for i in my_list if i=="two"]
i is iterated through the list my_list. As the list comprehension finishes evaluation, i is assigned to the last item in iteration, which is "three".
A "for ... if" statement - Ideas - Discussions on Python.org
If-else condition in one liner
How can I write this for loop in one line?
python - Single line for loop over iterator with an "if" filter? - Stack Overflow
What is the syntax for a one-line if else in Python?
Can I write true elif on a single line?
Is a list comprehension the same as a for loop?
Videos
There's this common syntax 'a' if condition else 'b' and I found it more readable and faster to write, but recently I ran into problem like this
# basically means if A exists, it covers the previous one, else prev becomes A. usually used within a loop.
lst = ['x', '', 'y']
prev = None
for x in lst:
if x:
prev = x
else:
x = prevSomehow I cannot write this into prev = x if x else x = prev .
I kind of get it why this is an error but I just wonder if there's a better way to write this kind of code?
Or, what is a more efficient way of writing this for loop?
Thanks in advance for your help.
for i, day in enumerate(week):
if i < 5:
week_fmt.append(day)
week_fmt.append(day)
elif i > 4:
week_fmt.append(day)
No, there is no shorter way. Usually, you will even break it into two lines :
important_airports = (airport for airport in airports if airport.is_important)
for airport in important_airports:
# do stuff
This is more flexible, easier to read and still don't consume much memory.
You could do:
for airport in filter(lambda x: x.is_important, airports):
# do stuff...