NEW TO THIS: Assigning values to strings in list?

Martin v. Loewis martin at v.loewis.de
Fri Jul 19 12:23:11 EDT 2002


Johannes Graumann <graumann at clyde.caltech.edu> writes:

> Basicely I have two lists, one containing the future string-names 
> and one containing the values I want to give them. The order is
> identical in both. Right now I'm solving this by a dictionary, but 
> I would really prefer to have straight strings as a result of my 
> operation.
> 
> Can anybody please tell me how to go about this and/or why I 
> totally shouldn't do this?

I'm not quite sure what "this" is that you want to go about, or what
"this" is that you want to solve. You say you have two lists - that,
in itself, is not a problem; Python allows you to have as many lists
as you want :-)

If you want to know how to associate strings and values, without using
a dictionary, I recommend to use a list of tuples, and the zip function

>>> strings = ['a', 'b', 'c']
>>> values  = [1, 2, 3]
>>> zip(strings, values)
[('a', 1), ('b', 2), ('c', 3)]

That has the advantage of preserving the order of the strings, but the
disadvantage that you can't use the string as a key easily.

Regards,
Martin



More information about the Python-list mailing list