I would like to reverse my list compreshsion code to (normal code = if that is the term used ) please. if anyone can help i have given the solution of the reverse below from normal code to list comprehension . if someone could do the same for question below .
#example normal code
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
#example list comprehension code
squares = [value**2 for value in range(1,11)]
print(squares)
Question
Use list comprehension and the zip function to create a new list named sums that sums corresponding items in lists z and x. For example, the first item in the new list should be 5 from adding 1 and 4 together.
#new_variable = [operations for temporary variable in list]
z = [1.0, 2.0, 3.0]
x = [4.0, 5.0, 6.0]
zipped = zip(x,z)
print(zipped)
[(1.0, 4.0), (2.0, 5.0), (3.0, 6.0)]
#list comprehension
sums = [item1 item2 for item1,item2 in ziped ]
print(sums)
ans :
[5.0, 7.0, 9.0]
CodePudding user response:
This is what they're asking for I believe:
z = [1.0, 2.0, 3.0]
x = [4.0, 5.0, 6.0]
zipped = zip(x,z)
sums = []
for item1, item2 in zipped:
result = item1 item2
sums.append(result)
print(sums)