We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,127 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Ways to create Dictionary

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

7
Contributors
11
Replies
7 Years
Discussion Span
3 Months Ago
Last Updated
37
Views
Question
Answered
bumsfeld
Nearly a Posting Virtuoso
1,495 posts since Jul 2005
Reputation Points: 409
Solved Threads: 235
Skill Endorsements: 1

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
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36

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
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36

... 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
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36

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,495 posts since Jul 2005
Reputation Points: 409
Solved Threads: 235
Skill Endorsements: 1

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

vegaseat
DaniWeb's Hypocrite
Moderator
6,475 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,611
Skill Endorsements: 36
Question Answered as of 3 Years Ago by vegaseat

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
Skill Endorsements: 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
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

wow wegaseat!!

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

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

Shows you the most common ways.


zzzzz... lol thread necro.

Enders_Game
Light Poster
25 posts since Mar 2010
Reputation Points: 23
Solved Threads: 6
Skill Endorsements: 0

I found another way:

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

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.

Umar Suleman
Newbie Poster
1 post since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3859 seconds using 2.67MB