1>> d = {'a': 'Arthur', 'b': 'Belling'}
2
3>> d.items()
4[('a', 'Arthur'), ('b', 'Belling')]
5
6>> d.keys()
7['a', 'b']
8
9>> d.values()
10['Arthur', 'Belling']
11
1my_list = ['Nagendra','Babu','Nitesh','Sathya']
2my_dict = dict()
3for index,value in enumerate(my_list):
4 my_dict[index] = value
5print(my_dict)
6#OUTPUT
7{0: 'Nagendra', 1: 'Babu', 2: 'Nitesh', 3: 'Sathya'}
1# This is our example dictionary
2petAges = {"Cat": 4, "Dog": 2, "Fish": 1, "Parrot": 5}
3# This will be our list, epmty for now
4petAgesList = []
5# Search through the dictionary and find all the keys and values
6for key, value in petAges.items():
7 petAgesList.append([key, value]) # Add the key and value to the list
8print(petAgesList) # Print the now-filled list
9# Output: [['Cat', 4], ['Dog', 2], ['Fish', 1], ['Parrot', 5]]
1# Python3 program to Convert a
2# list to dictionary
3
4def Convert(lst):
5 res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
6 return res_dct
7
8# Driver code
9lst = ['a', 1, 'b', 2, 'c', 3]
10print(Convert(lst))
1def to_dictionary(keys, values):
2 return dict(zip(keys, values))
3
4keys = ["a", "b", "c"]
5values = [2, 3, 4]
6print(to_dictionary(keys, values)) # {'a': 2, 'c': 4, 'b': 3}