Dictionaries

Sat 31 January 2015

Filed under Python

Dictionaries

ways to create the dictionary

>>> d = dict(zip(('A','B','C'),(1,2,3)))
>>> d
{'A': 1, 'C': 3, 'B': 2}
>>> e = {}
>>> e['A']= 1
>>> e
{'A': 1}
>>> e['B']=2
>>> e
{'A': 1, 'B': 2}
>>> e['C']=3
>>> e
{'A': 1, 'C': 3, 'B': 2}
>>> e= {}
>>> zipped = zip(('A','B','C'),(1,2,3))
>>> for key, value in zipped:
...   e[key] = value
... 
>>> e
{'A': 1, 'C': 3, 'B': 2}

Comments


DeeKras.com © Dee Kras Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More