how to get the value of key in python
print(dict.get("key"))
get dictionary value in python
my_dict = {'a': 1, 'b': 2, 'c': 3}
# keys = list(item.keys())
# the upper commented code give the list of the keys.
keys = ['a', 'b', 'c', 'd'] # if some body give the list of the keys that wasn't match with my_dict
for key in keys:
print(my_dict.get(key, 'default value when key not exist'))
"""output:
1
2
3
default value when key not exist
"""
get a value from a dictionary python
#!/usr/bin/python
dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" % dict.get('Age')
print "Value : %s" % dict.get('Education', "Never")
how to get element from dictionary python
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
python dictionary access value by key
# Create a list of dictionary
datadict = [{'Name': 'John', 'Age': 38, 'City': 'Boston'},
{'Name': 'Sara', 'Age': 47, 'City': 'Charlotte'},
{'Name': 'Peter', 'Age': 63, 'City': 'London'},
{'Name': 'Cecilia', 'Age': 28, 'City': 'Memphis'}]
# Build a function to access to list of dictionary
def getDictVal(listofdic, name, retrieve):
for item in listofdic:
if item.get('Name')==name:
return item.get(retrieve)
# Use the 'getDictVal' to read the data item
getDictVal(datadict, 'Sara', 'City') # Return 'Charlotte'
# -------------------
# to convert a dataframe to data dictionary
df = pd.DataFrame({'Name': ['John', 'Sara','Peter','Cecilia'],
'Age': [38, 47,63,28],
'City':['Boston', 'Charlotte','London','Memphis']})
datadict = df.to_dict('records')
how to get value from key dict in python
# 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
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us