[Tutor] dict(sequence) and dict(mapping)

antonmuhin at rambler.ru antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Sun Apr 13 12:24:09 2003


Hello Scott,

Sunday, April 13, 2003, 12:22:21 AM, you wrote:

SC> I'm looking at help(__builtins__) in Python 2.2.1.

SC> It shows:
SC>     class dict(object)
SC>      |  dict() -> new empty dictionary.
SC>      |  dict(mapping) -> new dictionary initialized from a mapping object's
SC>      |      (key, value) pairs.
SC>      |  dict(seq) -> new dictionary initialized as if via:
SC>      |      d = {}
SC>      |      for k, v in seq:
SC>      |          d[k] = v

SC> I can say:
SC>   test_dict = dict() 
SC> to create a empty dictionary.  
SC> How do I use the other two forms of this syntax:
SC> test_dict = dict(mapping)
SC> test_dict = dict(sequence)

SC> Please give me some sample mappings and sequences that work in this syntax 
SC> notation.

SC> Thanks!
SC> Scott

I hope the following example will be of help. Note, that dict creates
a copy of object and, therefore, differs from assignment.

PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> d = {'a': 1, 'b': 2}
>>> l = (('a', 1), ('b', 2))
>>> print dict(d)
{'a': 1, 'b': 2}
>>> print dict(l)
{'a': 1, 'b': 2}
>>> d_copy = dict(d)
>>> d_copy['c'] = 3
>>> d_copy
{'a': 1, 'c': 3, 'b': 2}
>>> d
{'a': 1, 'b': 2}
>>> 

-- 
Best regards,
 anton                            mailto:antonmuhin@rambler.ru