1.11M Members

Ways to create Dictionary

 
3
 

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

 
0
 

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}
 
1
 

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
 
0
 

... 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, ... }
 
1
 

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
 
0
 

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

Question Answered as of 4 Years Ago by vegaseat
 
0
 

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?

 
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.

 
0
 

wow wegaseat!!

 
1
 

I found another way:

d = dict(enumerate('abcd'))
print(d)  # {0: 'a', 1: 'b', 2: 'c', 3: 'd'}
 
0
 

i know you are saying right but i dont know how can i make a dictionary for my mobile. can you help me to create a own dictionary instead of made dictionary.

Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article