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

abdi1 commented: i need the best and easy one from those +0
kindo commented: Good! This Question made me to Join this forum.Good +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}

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

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

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

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

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?

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.

wow wegaseat!!

I found another way:

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.