Answers for "x for x in python"

31

python list comprehension

nums = [4, -7, 9, 1, -1, 8, -6]
half_of_nums = [x/2 for x in nums] #[2, -3.5, 4.5, 0.5, -0.5, 4, -3]

#optionally you can add an if statement like this
half_of_positive_nums = [x/2 for x in nums if x>=0] #[2, 4.5, 0.5, 4]
Posted by: Guest on May-20-2020
1

x for x in python

# This could be said as:
x for x in thing: 
# Or
for x in thing:
    return x # Or whatever that puts x in some variable

# Same thing, just really dodgy syntax.
Posted by: Guest on August-28-2020

Python Answers by Framework

Browse Popular Code Answers by Language