by
0
7
5,098
0
Top 1% !
Popular
Famous
Pearl of Wisdom
Tagged
Specified
OpenSource
Popularity: 1530th place
Created
Modified Sep 24, 2013

Published on:

Architecturex86_64
LanguagePython
LicenseMIT_X11

How to populate a dictionary in python

This code shows how to populate a dictionary, using the built-in method setdefault.

If the key does not exist, it will be created setting its value too. 

Otherwise the new value will be appended to the existing ones.

In this way the key existence in automatically checked by python during run time.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/11234/How-to-populate-a-dictionary-in-python?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
myDict= dict() keyName = 'myNewKey' txtData = 'some data' myDict.setdefault(keyName,[]).append(txtData)
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus