-2

For example that i have 2 list and i need to append them into a dictionary with list. i try the for loops but seems does not work. what should i do ?

keys = ('a','b','c')
values = ('1','2','3')

result = {}

for key in keys:
    for value in values:
        # result[key] = value // does not print out the result i want but instead adding the value to each key
        result[key].append(value) 

print(result)

# result = {'a':['1','2','3'],
#           'b':['1','2','3'],
#           'c':['1','2','3']}
Yogurt
  • 93
  • 1
  • 7
  • https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary Using built in functions. – Steffen Andersland Jul 01 '20 at 03:47
  • result = { k:list(values) for k in keys } – user120242 Jul 01 '20 at 03:51
  • 1
    I interpreted the question as saying that the dictionary in the comment at the bottom is the intended result. Is that correct? It's not the current result, as the current result is a KeyError. – Brian McCutchon Jul 01 '20 at 03:54
  • `result = dict(zip(keys,[list(values)]*3))` will also work, but note that they will all point to the same list, so it's probably not what you want – user120242 Jul 01 '20 at 03:56
  • @Brian right, otherwise he wouldn't be trying to use append, and commenting about "adding the value" not being what he wanted. zip is definitely not what this guy wants – user120242 Jul 01 '20 at 03:59
  • In that case, the question should be reopened. – Brian McCutchon Jul 01 '20 at 04:01
  • maybe it's just the wrong dupe link? It seems likely that this is a dupe of some thread somewhere. I wanted to delete my answer, and post answers as comment, but I'm no good at finding dupes. – user120242 Jul 01 '20 at 04:02
  • thank you, i should use the word convert / add 2 list into a dictionary with list. I did a search on the forum and the result was to convert 2 list into a dictionary with a key:value result and it was not something i want. But thank you for all the answer that now i realized it was simpler than i thought – Yogurt Jul 01 '20 at 04:02

2 Answers2

0

This will get your result:

keys = ('a','b','c')
values = ('1','2','3')
result = {}

for key in keys:
    result[key] = list(values)
print(result)

# result = {'a':['1','2','3'],
#           'b':['1','2','3'],
#           'c':['1','2','3']}
leopardxpreload
  • 767
  • 5
  • 17
0

A dictionary doesn't work quite like a list

keys = ('a','b','c')
values = ('1','2','3')
values = list(values)

result = {}

for key in keys:
    result.update({key:values})

print(result)

EDIT : Convert value tuple into a list, don't loop over the values list.

ZWang
  • 832
  • 5
  • 14
  • Have you tried it before posting? – DYZ Jul 01 '20 at 03:50
  • Oh wait I misread the question – ZWang Jul 01 '20 at 03:55
  • You need to make clear, that they will all be pointing to the same list reference. result['a']=5 will make result={a:[5,2,3],b:[5,2,3]...} – user120242 Jul 01 '20 at 04:00
  • @user120242 OP didn't request that though, he'd need deepcopys if that were the case – ZWang Jul 01 '20 at 04:02
  • Sure you can say that, but it's strongly implied if you convert a tuple to a list that there's a very high likelihood of needing to mutate the list, since there's few other reasons you would need to do the conversion. You only need shallow copies of the list. – user120242 Jul 01 '20 at 04:08