[Tutor] Creating a dictionary

Peter Otten __peter__ at web.de
Fri May 27 16:40:54 CEST 2011


Válas Péter wrote:

> I think I am new to here, as far as I remember. :-)
> 
> http://docs.python.org/dev/library/stdtypes.html#dict says:
> we can create a dictionary with
> 
>    - dict({'one': 1, 'two': 2})
> 
> What is the adventage of this form to simply writing d = {'one': 1, 'two':
> 2}? Is there any difference?

That is meant to illustrate that you can create a dictionary from another 
one by passing the existing dictionary to dict(). A more realistic example 
is

>>> friends = {"Jim": 42, "Jack": 18}
>>> family = {"Sue": 7, "Eli": 30}
>>> friends_and_family = dict(friends)
>>> friends_and_family.update(family)
>>> friends_and_family
{'Eli': 30, 'Sue': 7, 'Jim': 42, 'Jack': 18}




More information about the Tutor mailing list