python key value pair code example

Example 1: dictionary in python does not support append operation

dict_append = {"1" : "Python", "2" : "Java"}
dict_append.update({"3":"C++"})  # append doesn't supported in dict 
								 # instead , use update in dict
print(dict_append)
# output : {'1': 'Python', '2': 'Java', '3': 'C++'}

Example 2: python create dictionary from key value

>>> L1 = ['a','b','c','d']
>>> L2 = [1,2,3,4]
>>> d = dict(zip(L1,L2))
>>> d
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Example 3: python dictionary add key-value pair

dict = {'key1':'value_one', 'key2':'value_two'}  
dict['key3'] = 'value_three'

Example 4: dictionary in python

# Dictionaries in Python

ages = {"John": 43, "Bob": 24, "Ruth": 76} # Marked by { at beginning and a } at end

# ^^^ Has sets of keys and values, like the 'John' and 43 set. These two values must be seperated by a colon

# ^^^ Sets of values seperated by commas.

Example 5: python key value pair list

student = {"first_name": "Henry", "last_name": "Smith", "age": 20} # key: value

print(student["first_name"])
>>> Henry