Get Value From Dictionary Python

Published On - 3 May 2024

In this article we will show you the solution of get value from dictionary python, In Python, the dictionary is the combination of key-value pairs.

It is represented in curly braces. For example- {“1”: “Python”, “2”: “Java”} where 1, 2 represents the key and Python, Java represents the value of the key.

In dictionaries, values are integer, list, and tuple. In this topic we take input for a dictionary which means the user gives input for the dictionary then we apply methods.

We can use the get() method to get the value from the Dictionary and using dict[key]. dict[key] is used to access the value of the specific key. We also use dict.values() that gives the values from the dictionary where dict represents the dictionary name.

Step By Step Guide On Get Value From Dictionary Python :-

1. Take input from user and use dict.values() to get the value from Dictionary

a={}
for i in range(0,2):
    Number= input()
    language= input()
    a[Number]=language
#print(a.values()) #dict_values(['python', 'java'])
for i in a.values():
    print(i) #python, java
  1. We create an empty dictionary where all the input data from the user is added.
  2. We create a variable Number and language and use the input() method to take input from the user. We take two inputs in Number and language because the loop range goes from 0 to 2.
  3. Then we assign language in a[Number] so that it indicates in Number 1 the language is python and in Number 2 the language is java.
  4. We use a.values() method to get the value for the dictionary.

2. Take input from user and use get() to get the value from Dictionary

a={}
for i in range(0,2):
    Number= input()
    language= input()
    a[Number]=language
print(a)
#{'1': 'python', '2': 'java'}
print(a.get('1')) #python
print(a.get('2')) #java
  1. We create an empty dictionary where all the input data from the user is added.
  2. We create a variable Number and language and use the input() method to take input from the user. We take two inputs in Number and language because the loop range goes from 0 to 2.
  3. Then we assign language in a[Number] so that it indicates in Number 1 the language is python and in Number 2 the language is java.
  4. Then we use get() and pass ‘1’ and ‘2’ that indicate the key from the dictionary so, it gives the value from the key.

3. Take input from user and use dict[key] to get the value from Dictionary

a={}
for i in range(0,2):
    Number= input()
    language= input()
    a[Number]=language
print(a)
print(a['1']) #python
print(a['2']) #java
  1. We create an empty dictionary where all the input data from the user is added.
  2. We create a variable Number and language and use the input() method to take input from the user. We take two inputs in Number and language because the loop range goes from 0 to 2.
  3. Then we assign language in a[Number] so that it indicates in Number 1 the language is python and in Number 2 the language is java.
  4. Then we can use dict[key] to get the value from the dictionary where dict represents “a” and key represents “1” and “2”. In this code, it gets the value for these keys.

Conclusion :-

In conclusion, we successfully learned how to get value from a dictionary.

We learned various methods such as get(),dict[key], and dict. values(). You can choose any one of the methods that suits you.

I hope this article on get value from dictionary python helps you and the steps and method mentioned above are easy to follow and implement.