Home Coding Dictionary – GET, ITEMS, KEYS, VALUES

Dictionary – GET, ITEMS, KEYS, VALUES

0
Dictionary – GET, ITEMS, KEYS, VALUES

# Continue our work with dictionaries
#Today we try to extract some data from a dictionary
#
# GET - ITEMS - KEYS - VALUES

# Just create a dictionary
my_dict = {"a":"Peter","b":"Anna","c":"Mark", "d":"Jeff", "e":"Grace"}

# To retrieve a value from a dictionary we use the method .get()
# To use .get() we have to insert a key in the round parenthesis 
out1 = my_dict.get("a")

# With .items() methods you will retrive a list of all key:values pairs of the dictionary
out2 = my_dict.items()

# With .keys() you will retive a list of all keys of the dictionary
out3 = my_dict.keys()

# With .values()
out4 = my_dict.values()

# copy www.AEC.codes
OUT = out1,out2,out3,out4


Extra

If you try to update a dictionary or add a key:value pair to it, be sure that the key is not present in the dictionary.

Otherwise, you will update the value for the key already present and not add a new key:value pair.

Another way to create a new dictionary is using the dict() function.

For example : myNewDict = dict()