How To View Dictionary In Python

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:

Here’s an example to access the value using a key:

This will output:

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).

This will output:


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.

– Using dict.values(): This returns a view object that displays a list of all the values in the dictionary.

– Using dict.items(): This returns a view object that displays a list of the dictionary’s (key, value) tuple pairs.

Here’s an example to demonstrate the above methods:

This will output:


New York

Key-Value Pairs:
name : John
age : 30
city : New York

Full Code


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.