In this tutorial, we will learn how to view a dictionary in Python. A dictionary is a collection of key-value pairs, where each key is unique and can be used to access the corresponding value.
Dictionaries are mutable, and unordered, and can store data in a flexible and efficient way. In this tutorial, we will cover different methods to view and access the elements of a dictionary.
1. Accessing an Element in a Dictionary
To access a specific value in a dictionary, you can use the key as an index, like this:
1 |
dict_name[key] |
Here’s an example to access the value using a key:
1 2 3 |
my_dictionary = {"name": "John", "age": 30, "city": "New York"} print(my_dictionary["name"]) print(my_dictionary["city"]) |
This will output:
John New York
2. Using the get() Method
You can also use the **get()** method to access the value associated with a specific key. If the key is not found, it returns the default value specified as the second argument to the function (None, if not specified explicitly).
1 2 3 4 |
my_dictionary = {"age": 30, "city": "New York", "name": "John"} print(my_dictionary.get("name")) print(my_dictionary.get("country")) print(my_dictionary.get("country", "USA")) |
This will output:
John None USA
3. Looping Over a Dictionary
You can loop over the keys, values, or key-value pairs of a dictionary using a for loop. Here are the different methods for looping:
– Using dict.keys(): This method returns a view object that displays a list of all the keys in the dictionary.
1 2 |
for key in dict_name.keys(): print(key) |
– Using dict.values(): This returns a view object that displays a list of all the values in the dictionary.
1 2 |
for value in dict_name.values(): print(value) |
– Using dict.items(): This returns a view object that displays a list of the dictionary’s (key, value) tuple pairs.
1 2 |
for key, value in dict_name.items(): print(key, value) |
Here’s an example to demonstrate the above methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
my_dictionary = {"name": "John", "age": 30, "city": "New York"} print("Keys:") for key in my_dictionary.keys(): print(key) print("\nValues:") for value in my_dictionary.values(): print(value) print("\nKey-Value Pairs:") for key, value in my_dictionary.items(): print(key, ":", value) |
This will output:
Keys: name age city Values: John 30 New York Key-Value Pairs: name : John age : 30 city : New York
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
my_dictionary = {"name": "John", "age": 30, "city": "New York"} # Accessing elements using key print(my_dictionary["name"]) print(my_dictionary["city"]) # Accessing elements using get() method print(my_dictionary.get("name")) print(my_dictionary.get("country")) print(my_dictionary.get("country", "USA")) # Looping over a dictionary print("\nKeys:") for key in my_dictionary.keys(): print(key) print("\nValues:") for value in my_dictionary.values(): print(value) print("\nKey-Value Pairs:") for key, value in my_dictionary.items(): print(key, ":", value) |
Conclusion
In this tutorial, we learned about different ways to view a dictionary in Python: accessing elements using a key, the get() method, and looping over keys, values, or key-value pairs. Using these methods, you can easily view and access the data stored in your dictionaries.