0

Simply, I am trying to fill up the dictionary from two lists, the first list has treated as the keys where all have the same word "text". And the second list has treated as values.

List_1 = ["text", "text", "text"]

List_2 = ["Programmer", "Engineer", "Art Therapist"]

data_dict = {}
for j in range(len(List_1)):
    data_dict[List_1[j]] = List_2 [j]

print(data_dict)


#Expected output:
{'text': 'Programmer', 'text': 'Engineer', 'text': 'Art Therapist'}

#What I am getting is only:
{'text': 'Art Therapist'}

Why overwriting is happening? Can anyone help with this?

Sociopath
  • 13,068
  • 19
  • 47
  • 75
Mohsen Ali
  • 655
  • 1
  • 9
  • 30

1 Answers1

1

You are using the wrong data structure for what you want to achieve, since dict keys must be unique.

You could either zip() the two lists together (but this will still be a sequence / list):

list(zip(List_1, List_2))
# [('text', 'Programmer'), ('text', 'Engineer'), ('text', 'Art Therapist')]

or create a custom class implementing the functionalities you were expecting from the dict(). See Make a dictionary with duplicate keys in Python for more info.

norok2
  • 25,683
  • 4
  • 73
  • 99