Exercise 116
From the following two lists, create a dictionary by two different methods:
keys = ['Name', 'Email', 'Phone']
values = ['Albert', 'albert@gmail.com', 2225668877]
The output should be:
data = {'Name': 'Albert', 'Email': 'albert@gmail.com', 'Phone': 2225668877}
Solution
First method
keys = ['Name', 'Email', 'Phone']
values = ['Albert', 'albert@gmail.com', 2225668877]
data = dict(zip(keys, values))
print(data)
Sconde method
keys = ['Name', 'Email', 'Phone']
values = ['Albert', 'albert@gmail.com', 2225668877]
# crate an empty dictionary
data = dict({})
# building th dictionary
for i in range(0, len(keys) ):
data[keys[i]] = values[i]
print(data)
Younes Derfoufi
my-courses.net
my-courses.net