Shapol46
0
Q:

get values from dictionary python

#!/usr/bin/python

dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Education', "Never")
1
dic = {"A":1, "B":2} 
print(dic.get("A")) 
print(dic.get("C")) 
print(dic.get("C","Not Found ! ")) 
4
dict = {'color': 'blue', 'shape': 'square', 'perimeter':20}
dict.get('shape') #returns square

#You can also set a return value in case key doesn't exist (default is None)
dict.get('volume', 'The key was not found') #returns 'The key was not found'
8
d.values()
2
print(dict.get("key"))
1
# Get a value from a dictionary in python from a key

# Create dictionary
dictionary = {1:"Bob", 2:"Alice", 3:"Jack"}

# Retrieve value from key 2
entry = dictionary[2]

# >>> entry
# Alice
-1

New to Communities?

Join the community