1

I currently have 3 lists and an empty dictionary:

dict = {}

name = ['Bob', 'Amy', 'Jack']
gender = ['Male', 'Female', 'Male']
car = ['kia', 'ford', 'audi']
country = ['UK', 'US', 'Australia']

Can I use a for loop in order to create dictionary entries? So ideally I'd end up with:

dict = {'Bob': {'gender':'Male', 'car':'kia', 'country':'UK'},
        'Amy': {'gender':'Female', 'car':'ford', 'country':'US'},
        'Jack': {'gender':'Male', 'car':'audi', 'country':'Australia'}}

2 Answers 2

2

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'}
Sign up to request clarification or add additional context in comments.

Comments

0

Use zip and a dictionary comprehension:


dct = {n: {'gender': g, 'car': k, 'country': c}
       for n, g, k, c in zip(name, gender, car, country)}

As a side note, avoid naming your variables using builtin names in Python, such as dict (I am using dct instead, but a better name could be people or, even better, something you can think of that fits your actual use case).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.