Quantcast
Viewing all articles
Browse latest Browse all 10

Using if/else combination in a list comprehension in Python

Okay, you might know how to use conditionals in a list comprehension in Python, like so: original_list=[1,2,3,4,5,6,7,8,9] evens=[x for x in original_list if x%2==0] odds=[x for x in original_list if x%2!=0] This would create two lists, containing the even numbers and the odd numbers in original_list. Now, using if/else is slightly different. Lets say you [...]

Viewing all articles
Browse latest Browse all 10

Trending Articles