Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Accessing Elements from Dictionary

# get vs [] for retrieving elements
my_dict = {'name': 'Jack', 'age': 26}

# Output: Jack
print(my_dict['name'])

# Output: 26
print(my_dict.get('age'))

# Trying to access keys which doesn't exist throws error
# Output None
print(my_dict.get('address'))

# KeyError
print(my_dict['address'])
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Accessing #Elements #Dictionary
ADD COMMENT
Topic
Name
3+8 =