Creating variables from dicts

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Feb 24 12:38:10 CET 2010


Luis M. González a écrit :
> On Feb 23, 5:53 pm, vsoler <vicente.so... at gmail.com> wrote:
>> Hi,
>>
>> I have two dicts
>>
>> n={'a', 'm', 'p'}
>> v={1,3,7}
>>
>> and I'd like to have
>>
>> a=1
>> m=3
>> p=7
>>
>> that is, creating some variables.
>>
>> How can I do this?
> 
> You are probably coming from another language and you're not used to
> python's data structures.
> If you want a list of items, you use tuples or lists.

<pedantic>

If you want a list, then you use a list - not a tuple !-)

> Examples:
> 
>     ('a', 'm', 'p') ---> this is a tuple, and it's made with
> parenthesis ()

It's not the parens that "make" the tuple, it's the commas:

 >>> t1 = (1, 2, 3)
 >>> t2 = 1, 2, 3
 >>> t1
(1, 2, 3)
 >>> t2
(1, 2, 3)
 >>> type(t2)
<type 'tuple'>
 >>>

The only case where the parens are required is to define an empty tuple:
 >>> empty = ()
 >>> empty
()
 >>> type(empty)
<type 'tuple'>

</<pedantic>

Now it's most of the time a good idea to still use the parens since it 
makes for more readable code (IMHO...)

(snip  helpful content)



More information about the Python-list mailing list