-1

I have two lists as:

num_list = [1,2,3,4]

name_list = ["one","two","three","four"]

I want to create a new dictionary with name_list as keys and num_list as values. I'm aware of zip method but I'm trying to do it using for loop for my own learning . What I've tried as:

new={}
num_list = [1,2,3,4]
name_list = ["one","two","three","four"]
for i in (name_list):
    for j in (num_list):
        new[i]=j

getting ouptput as:

{'one': 4, 'two': 4, 'three': 4, 'four': 4}

Can anyone explain where I did a mistake??

ncica
  • 7,015
  • 1
  • 15
  • 37
Learner
  • 800
  • 1
  • 8
  • 23
  • 1
    When you call the second for loop you are traversing through the entirety of `num_list` hence after your code executes, for each value in `name_list` gets assigned as a key-value pair with the last element of `num_list`, in this case `4` – sudo97 Jul 08 '19 at 12:32
  • 1
    Possible duplicate of https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary – Light Yagami Jul 08 '19 at 12:57
  • You can check this answer https://stackoverflow.com/a/15709950/8630546 – E.Praneeth Jul 08 '19 at 12:59

3 Answers3

2

You're using nested for-loops. For each i in name_list, and for each j in num_list, you're adding one element in dictionary new. So, in the end, you're adding 4*4 = 16, key, value pairs into the dictionary.

You can do it in this way:

new={}
num_list = [1,2,3,4]
name_list = ["one","two","three","four"]
for i in range(len(name_list)):
    new[name_list[i]]=num_list[i]

This question is similar to https://stackoverflow.com/a/15709950/8630546

E.Praneeth
  • 264
  • 3
  • 16
0

in second for loop for every key you are iterating over all values from num_list, because of that for each key you have last value(4) from num_list

you can just do:

num_list = [1,2,3,4]
name_list = ["one","two","three","four"]

print (dict([[y,num_list[x]] for x,y in enumerate(name_list)]))

output:

{'one': 1, 'two': 2, 'three': 3, 'four': 4}

or:

num_list = [1,2,3,4]
name_list = ["one","two","three","four"]

print ({name_list[i]:num_list[i] for i in range(len(num_list))})

output:

{'one': 1, 'two': 2, 'three': 3, 'four': 4}

If you want to use your code:

new={}
num_list = [1,2,3,4]
name_list = ["one","two","three","four"]
for i in (name_list):
    for j in (num_list):
        new[i]=j
        num_list.remove(j) # <-----
        break # <-----

print (new)

output:

{'one': 1, 'two': 2, 'three': 3, 'four': 4}

NOTE: you are missing two lines of code

ncica
  • 7,015
  • 1
  • 15
  • 37
0

To get an understand the error we have to dry-run your code. Let's do it, I copy-paste your original code here. Othewise you have to scroll screen.

new={}
num_list = [1,2,3,4]
name_list = ["one","two","three","four"]
for i in (name_list):
    for j in (num_list):
        new[i]=j

Dry-Run

+-----+-----+-------------+
|  i  |  j  |   new[i]=j  |
+-----+-----+-------------+
| one |  1  |  {'one':1}  |
---------------------------
|     |  2  |  {'one':2}  |
---------------------------
|     |  3  |  {'one':3}  |
---------------------------
|     |  4  |  {'one':4}  |
---------------------------

I dry-run only for completing first round. So your 2nd for loop, looping 4 times. End of the 4th times your j value always 4. That's why all the values in your dictionary became 4. Can I suggest some easy-step? Your num_list and name_list both length is 4. So try this if you want.

for i in range(4):
    new[name_list[i]] = num_list[i]
print(new)

# or using dict comprehension

print({(name_list[i]): (num_list[i]) for i in range(4)})

output:

{'one': 1, 'two': 2, 'three': 3, 'four': 4}
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58