954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Ways to create Dictionary

There seems to be many ways to create a dictionary, which one do you prefer?

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

For those who want to minimize typing ...

# this saves you from having to add quotes around any strings/characters (keys only)
dic1 = dict(a=1, b=2, c=3, d=4)

print dic1   # {'a': 1, 'c': 3, 'b': 2, 'd': 4}
vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

Let's say you want to create an English to French dictionary. You go to babelfish.altavista.com and enter "one, two, three, four, five, six, seven, eight, nine, ten" tell it to translate that from English to French and it gives you "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix". The easy way to make a dictionary from this after copy and paste ...

str1 = "one, two, three, four, five, six, seven, eight, nine, ten"
# after babelfish.altavista.com  English to French translation
str2 = "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix"

engList = str1.split(", ")
frenList = str2.split(", ")
eng2frenDict = {}
k = 0
for eng in engList:
    eng2frenDict[eng] = frenList[k]
    k += 1
    
print eng2frenDict
""" result =
{'seven': 'sept', 'ten': 'dix', 'nine': 'neuf', 'six': 'six', 'three': 'trois', 
'two': 'deux', 'four': 'quatre', 'five': 'cinq', 'eight': 'huit', 'one': 'un'}
"""

The same thing even shorter ...

str1 = "one, two, three, four, five, six, seven, eight, nine, ten"
str2 = "un, deux, trois, quatre, cinq, six, sept, huit, neuf, dix"

engList = str1.split(", ")
frenList = str2.split(", ")
eng2frenDict = dict(zip(engList, frenList))
print eng2frenDict
vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

... just one more way to create a dictionary, this one does a character count in a text string and gives you a dictionary of char:count pairs ...

# count the characters in a string and create a dictionary of char:count pairs

str1 = 'supercalifragilisticexpialidocious'
print "Count the characters in:  %s" % str1
charCount = {}
for char in str1:
    charCount[char] = charCount.get(char, 0) + 1
    
print charCount  # {'a': 3, 'c': 3, 'e': 2, 'd': 1, 'g': 1, ... }
vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

I think an empty dictionary can be created with:

emptyD1 = {}
emptyD2 = dict()
print emptD1, emptyD2

Then is the normal perhaps called direct way:

dict1 = {'a':1, 'b':2, 'c':3, 'd':4}  # etc.
print dic1

Does this way have a name?

dic2 = {}
dic2['Linux'] = 'software'
dic2['floppy'] = 'accessory'
dic2['floppy drive'] = 'hardware'
dic2['CPU'] = 'hardware'     # etc.
print dic2
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Your last way is officially called "using indexing and assignment".

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 
There seems to be many ways to create a dictionary, which one do you prefer?

i'm a student and i want to create an electronic dictionary, can you help me?

ajipw
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
i'm a student and i want to create an electronic dictionary, can you help me?


You should start your own thread instead of reviving an old one, and also tell us what you mean by 'create an electronic dictionary', which is not exactly the same thing as a python dictionary.

Gribouillis
Posting Maven
Moderator
2,781 posts since Jul 2008
Reputation Points: 1,024
Solved Threads: 691
 

wow wegaseat!!

babyash
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

http://docs.python.org/library/stdtypes.html#typesmapping

Shows you the most common ways.


zzzzz... lol thread necro.

Enders_Game
Newbie Poster
23 posts since Mar 2010
Reputation Points: 23
Solved Threads: 6
 

I found another way:

d = dict(enumerate('abcd'))
print(d)  # {0: 'a', 1: 'b', 2: 'c', 3: 'd'}
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You