How to get value in dictionary python

Code examples

10
0

python dict

# decleration
my_dict = {
  'spam': 'eggs',
  'foo': 4,
  100: 'bar',
  2: 0.5
}

# access single values from the dictionary
print(my_dict['spam']) # eggs
print(my_dict['foo']) # 4
print(my_dict[100]) # bar
print(my_dict[2]) # 0.5

# iterate over the dictionary
for key, value in my_dict.items():
  print(key, value)

# get length of the dictionary
print(len(my_dict)) # 4

# modify the dictionary
my_dict['baz'] = 'qux' # adds a pair
my_dict['baz'] = 'quxx' # also updates it
del my_dict['spam'] # removes a pair

# other methods
print(my_dict.copy()) # Returns a copy of the dictionary
print(my_dict.fromkeys('added', 100)) # Returns a dictionary with the specified keys and their values
print(my_dict.get('foo')) # Returns the value of the specified key
print(my_dict.items()) # Returns a list containing a tuple for each key value pair
print(my_dict.keys()) # Returns a list containing the dictionaries keys
print(my_dict.values()) # Returns a list of all the values in the dictionary
my_dict.setdefault('a', 'b') # Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
my_dict.pop('foo') # Removes the element with the specified key
my_dict.popitem() # Removes the last inserted key-value pair
my_dict.update({'baz': 'val'}) # Updates the dictionary with the specified key-value pairs
my_dict.clear() # Removes all the elements from the dictionary
9
0

python get value from dictionary

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'
1
0

how to get the value of key in python

print(dict.get("key"))
0
0

python dictionary get value

#!/usr/bin/python

dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Education', "Never")

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................