This article mainly introduces how to add entries to a dictionary using python. it is a practical dictionary operation technique and provides simple annotations for examples, for more information about how to add a dictionary entry using python, see the following example. Share it with you for your reference.
The specific implementation method is as follows:
Def addWord (theIndex, word, pagenumber): theIndex. setdefault (word, []). append (pagenumber) # adds the list to the list if it exists. if it does not exist, a new dictionary key d = {"hello": [3]} # d = {} addWord (d, "hello", 3) addWord (d, "hello", 56) addWord (d, "nihao", 24) print d
The test environment in this article is Python2.7.6
The program running result is as follows:
{'Nihao': [24], 'Hello': [3, 3, 56]}
I hope this article will help you learn Python programming.