1,105,427 Community Members

Ways to create Dictionary

Member Avatar
bumsfeld
Posting Virtuoso
1,537 posts since Jul 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 261 [?]
Skill Endorsements: 7 [?]
 
3
 

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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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}
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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, ... }
Member Avatar
bumsfeld
Posting Virtuoso
1,537 posts since Jul 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 261 [?]
Skill Endorsements: 7 [?]
 
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
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

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

Question Answered as of 4 Years Ago by vegaseat
Member Avatar
ajipw
Newbie Poster
1 post since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
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.

Member Avatar
babyash
Newbie Poster
1 post since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

wow wegaseat!!

Member Avatar
Enders_Game
Light Poster
25 posts since Mar 2010
Reputation Points: 13 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Shows you the most common ways.


zzzzz... lol thread necro.

Member Avatar
bumsfeld
Posting Virtuoso
1,537 posts since Jul 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 261 [?]
Skill Endorsements: 7 [?]
 
1
 

I found another way:

d = dict(enumerate('abcd'))
print(d)  # {0: 'a', 1: 'b', 2: 'c', 3: 'd'}
Umar Suleman
Newbie Poster
1 post since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
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.

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