I need to assign a result of for loop to a variable. It is easy to print the for loop in a single line using end =" "
, but I am unable to assign the result as a single line.
a = [1, 2, 3, 4]
for i in range(4):
print(a[i], end =" ")
Result is 1 2 3 4
But I have to assign the result to a variable as var = 1234
.
I am trying below way which is incorrect and not providing the result:
a = [1, 2, 3, 4]
for i in range(4):
var = print(a[i], end =" ")
print(var) # It provides the result in multiple line
What can I try next?