[Tutor] Pass variable on command line
Terry Carroll
carroll at tjc.com
Thu Jul 26 21:34:17 CEST 2007
On Thu, 26 Jul 2007, jason wrote:
> Hello,
>
> I have a situation where I have 2 lists
>
> List1 = ['blue', 'red', green']
> List2 = ['red', 'yellow', 'orange']
>
> And I would like to pass the list name on the command line like so
>
> ./test.py List1
>
> I know I can get the argument using sys.argv[1]
>
> But how can I then use the values in that list inside my program?
If you must do this, make a dictionary of the lists:
d = {"List1": List1, "List2": List2}
and index into it from your argument:
l = d[sys.argv[1]]
More information about the Tutor
mailing list