Python DictionariesA dictionary is a set of unordered key, value pairs. In a dictionary, the keys must be unique and they are stored in an unordered manner. Show
In this tutorial you will learn the basics of how to use the Python dictionary. By the end of the tutorial you will be able to - Create Dictionaries - Get values in a Dictionary - Add and delete elements in a Dictionary - To and For Loops in a Dictionary Creating a DictionaryLet’s try to build a
profile of three people using dictionaries. To do that you separate the key-value pairs by a colon(“:”). The keys would need to be of an immutable type, i.e., data-types for which the keys cannot be changed at runtime such as int, string, tuple, etc. The values can be of any type. Individual pairs will be separated by a comma(“,”) and the whole thing will be enclosed in curly braces( For example, you can have the fields “city”, “name,” and “food” for keys in a dictionary and assign the key,value pairs to the dictionary variable person1_information.
Get the values in a DictionaryTo get the values of a dictionary from the keys, you can directly reference the keys. To do this, you enclose the key in brackets So, in the following example, a dictionary is initialized with keys “city”, “name,” and “food” and you can retrieve the value corresponding to the key “city.”
You can also use the get method to retrieve the values in a dict. The only difference is that in the get method, you can set a default value. In direct referencing, if the key is not present, the interpreter throws KeyError.
Looping over dictionarySay, you got a dictionary, and you want to print the keys and values in it. Note that the key-words
Add elements to a dictionaryYou can add elements by updating the dictionary with a new key and then assigning the value to a new key.
Or you can combine two dictionaries to get a larger dictionary using the update method.
Delete elements of a dictionaryTo delete a key, value pair in a dictionary, you can use the
A disadvantage is that it gives KeyError if you try to delete a nonexistent key.
So, instead of the
More facts about the Python dictionaryYou can test the presence of a key using the
A dictionary in Python doesn't preserve the order. Hence, we get the following:
You see that even though "snape" was given later, it appeared at the top. Contributed by: Joydeep Bhattacharjee Which method would you use to get the value associated with a specific key and remove?The pop method accepts a key as an argument, returns the value that is associated with that key, and removes that key-value pair from the dictionary.
Which dictionary method returns the value associated with a specified key if the key is not found it returns a default value?Python Dictionary get() Method Syntax:
key: The key name of the item you want to return the value from. Value: (Optional) Value to be returned if the key is not found. The default value is None.
Which of the following methods removes all of the key value pairs from a dictionary called D?The popitem method returns a randomly selected key-value pair, as a tuple, and removes that key-value pair from the dictionary.
What does the get () method do if the specified key is not found in the dictionary?What does the get method do if the specified key is not found in the dictionary? It returns a default value.
|