You can use zip and dict comprehension:
name = ['Bob', 'Amy', 'Jack']
gender = ['Male', 'Female', 'Male']
car = ['kia', 'ford', 'audi']
country = ['UK', 'US', 'Australia']
db = {name: dict(zip(['gender', 'car', 'country'], rest))
for name, *rest
in zip(name, gender, car, country)
}
print(db)
Output:
{'Bob': {'gender': 'Male', 'car': 'kia', 'country': 'UK'},
'Amy': {'gender': 'Female', 'car': 'ford', 'country': 'US'},
'Jack': {'gender': 'Male', 'car': 'audi', 'country': 'Australia'}}
Unpacked it looks like:
for data in zip(name, gender, car, country):
n, g, ca, co = data
db[n] = {
'gender' : g,
'car' : ca,
'country': co
}
If we see, what the zip returns:
>>> for data in zip(name, gender, car, country):
print(data)
('Bob', 'Male', 'kia', 'UK')
('Amy', 'Female', 'ford', 'US')
('Jack', 'Male', 'audi', 'Australia')
Now, we can unpack these tuples (see iterable unpacking:
>>> n, g, ca, co = ('Bob', 'Male', 'kia', 'UK')
>>> n
'Bob'
>>> g
'Male'
>>> ca
'kia'
>>> co
'UK'
# Or we can take the name separately (as shown in dict comprehension):
>>> name, *rest = ('Bob', 'Male', 'kia', 'UK')
>>> name
'Bob'
>>> rest
['Male', 'kia', 'UK']
Now, let's take a look at how we can make a dict by calling dict:
>>> dict(a=2, b=3)
{'a': 2, 'b': 3}
# or if we have a bunch of tuples, for example:
>>> items = [('a', 2), ('b', 3)]
>>> dict(items)
{'a': 2, 'b': 3}
# so it is evident, it takes key-value pairs, so:
# and we supply, keys = ['gender', 'car', 'country']
# if we have, rest = ['Male', 'kia', 'UK'], which are the values
# we can zip them together to get key value pairs:
>>> list(zip(keys, rest))
[('gender', 'Male'), ('car', 'kia'), ('country', 'UK')]
# now we can call dict
>>> dict(zip(keys, rest))
{'gender': 'Male', 'car': 'kia', 'country': 'UK'}