Assign values from list to list of instances

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Tue Nov 1 11:40:52 EDT 2011


Am 01.11.2011 16:05, schrieb Gnarlodious:
> I want to assign a list of variables:
> locus=[-2, 21, -10, 2, 12, -11, 0, 3]
>
> updating a list of objects each value to its respective instance:
>
> for order in range(len(Orders)):
> 	Orders[order].locus=locus[order]
>
> This works, even though it reads like doggerel. Is there a more
> pythonesque way using map or comprehension?

Use enumerate:

for object, index in enumerate(Orders):
     object.locus = locus[index]


I'm not 100% I understood your description though, but it looks like 
this would do what you want and be descriptive at the same time.

Uli



More information about the Python-list mailing list