Answers for "list comprehension for loop"

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
11

list comprehension

# List comprehension 

        
list_comp = [i+3 for i in range(20)]

# above code is similar to 

for i in range(20):
	print(i + 3)
Posted by: Guest on August-11-2020
0

list comprehension python one line

doubled_odds = [n * 2 for n in numbers if n % 2 == 1]
Posted by: Guest on March-17-2020
0

list comprehension for loop

matrix = [[1, 2], [3,4], [5,6], [7,8]]
transpose = [[row[i] for row in matrix] for i in range(2)]
print (transpose)
Posted by: Guest on August-03-2020

Code answers related to "list comprehension for loop"

Python Answers by Framework

Browse Popular Code Answers by Language