I was trying to do an assignment operation in python single line for loop like
[a[i]=b[i] for i in range(0,len(b))]
This seems wrong. Is there a way I can use assignment operation in python single line for loop?
I was trying to do an assignment operation in python single line for loop like
[a[i]=b[i] for i in range(0,len(b))]
This seems wrong. Is there a way I can use assignment operation in python single line for loop?
You are mixing two paradigms here, that of loop and list comprehension. The list comprehension will be
a = [x for x in b]
Copy lists can be done in many ways in Python.
Using slicing
a = b[:]
Using list()
a = list(b)
You could use:
[a.__setitem__(i, b[i]) for i in range(0,len(b))]
the __setitem__
is the method that implements index-assignment. However doing a list comprehension only because of side-effects is a big no-no.
You could simply use slice-assignment:
a[:len(b)] = b