Thursday, 15 May 2014

Append several variables to a list in Python -



Append several variables to a list in Python -

i want append several variables list. number of variables varies. variables start "volume". thinking maybe wildcard or it. couldn't find this. ideas how solve this? note in illustration 3 variables, 5 or 6 or anything.

volumea = 100 volumeb = 20 volumec = 10 vol = [] vol.append(volume*)

you can utilize extend append iterable list:

vol.extend((volumea, volumeb, volumec))

depending on prefix of variable names has bad code smell me, can it. (the order in values appended undefined.)

vol.extend(value name, value in locals().items() if name.startswith('volume'))

if order of import (imho, still smells wrong):

vol.extend(value name, value in sorted(locals().items(), key=lambda item: item[0]) if name.startswith('volume'))

python list append

No comments:

Post a Comment