Append several variables to a list in Python
Question: I want to append several variables to a list. The number of variables varies.
All variables start with "volume". I was thinking maybe a wildcard or
something would do it. But I couldn't find anything like this. Any ideas how
to solve this? Note in this example it is three variables, but it could also
be five or six or anything.

    
    
    volumeA = 100
    volumeB = 20
    volumeC = 10
    
    vol = []
    
    vol.append(volume*)
    


Answer: You can use `extend` to append any iterable to a list:

    
    
    vol.extend((volumeA, volumeB, volumeC))
    

Depending on the prefix of your variable names has a bad code smell to me, but
you can do it. (The order in which values are appended is undefined.)

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

If order is important (IMHO, still smells wrong):

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

