1

I've got a List with their elements

my_list = ['a', 'b', 'c']

and a Dictionary that has their keys and blank strings as their values

my_dictionary = {
    'key_a' : '',
    'key_b' : '',
    'key_c' : '',
}

The question is: what is the pythonic way to put each one of this list's elements (in this particularly order) as each one of those dictionary values?

Result should be like this:

my_dictionary = {
    'key_a' : 'a',
    'key_b' : 'b',
    'key_c' : 'c',
}

Ps: please note that I do not need the dictionary values to be in order after that copy. As well pointed in the comments, after Python 3.7, dictionary's order is not guaranteed anymore

  • 2
    What is the relationship between the list elements and the dictionary keys? Same order, or same `key_*` suffix, or … ? – Jens Feb 29 '20 at 06:08
  • 1
    Only for your example, `my_dictionary.update({f"key_{k}": k for k in my_list})` works. – Boseong Choi Feb 29 '20 at 06:11
  • I have given the code that might be simple to understand. Don't forget to mark as accepted if it helped. – Nishant Agarwal Feb 29 '20 at 06:16
  • @Jens I am scrapping a restaurant's menu and the scrapped menu result is a list. But I need each element of the menu cataloged and categorized into this dictionary i've created first – Vitor Mafra Feb 29 '20 at 06:22
  • What Python version are you using? Prior to Python 3.7, dictionary order is not guaranteed. – MisterMiyagi Feb 29 '20 at 17:00
  • @MisterMiyagi I'm using 3.7, but i don't need the dictionary keys to be in order after that copy. I'll edit my question to make this point more clear – Vitor Mafra Mar 01 '20 at 17:25

4 Answers4

3

You can try this.

dict(zip(my_dictionary,my_list))

Use .update method to update your my_dictionary.


Output

{'key_a': 'a', 'key_b': 'b', 'key_c': 'c'}

Note: This works for python3.7 or above.

The reason is stated in the comments by @MisterMiyagi.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • This is only guaranteed to provide the expected result since Python 3.7. Prior to this, dictionary key order is not guaranteed (though it is de-facto ordered on CPython3.6 and PyPy3). – MisterMiyagi Feb 29 '20 at 16:59
  • 1
    @MisterMiyagi Agreed, I should add that detail to my answer. Adding it right now. – Ch3steR Feb 29 '20 at 17:00
  • Note that `zip` iterates for its shortest iterable. – Boseong Choi Mar 01 '20 at 16:47
  • Why the downvote you too posted the same answer? I would love to know the reason for downvoting @BoseongChoi – Ch3steR Mar 01 '20 at 16:48
  • It's not same. Your answer is for overwriting not updating. I think you can change `dict` to `my_dictionary.update`. – Boseong Choi Mar 01 '20 at 16:56
  • I just posted this as an alternative to all the answers posted it's up to OP discretion on which answer to choose. If OP wants to update in-place he can use `.update`, if he wants different dictionary he can use `dict(zip(...))` @BoseongChoi – Ch3steR Mar 01 '20 at 17:00
  • @Ch3steR Oh, I am sorry. I concluded too early so I didn't realize that your answer could be applied that way. I canceled downvote. Have a nice day, too. – Boseong Choi Mar 01 '20 at 17:08
1

I assume that your dictionary order is guaranteed or you don't care the order.
You can use built-in function zip.
It is very useful function to iterate two different iterables.
documentation: https://docs.python.org/3/library/functions.html#zip

my_dictionary = {
    'key_a': '',
    'key_b': '',
    'key_c': '',
}

my_list = ['a', 'b', 'c']

for key, item in zip(my_dictionary, my_list):
    my_dictionary[key] = item

print(my_dictionary)

output: {'key_a': 'a', 'key_b': 'b', 'key_c': 'c'}

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Boseong Choi
  • 2,566
  • 9
  • 22
  • 1
    ps. Please be careful with built-in `dict`'s order. See https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6. – Boseong Choi Feb 29 '20 at 06:28
  • That is a exactly what I was trying to find! Thank you very much :) And about the dictionary order: recently I've discovered that, but the order matters to me in this case only cause I needed to put each list element inside an especific dict key - after that, the dict's order doens't care anymore – Vitor Mafra Feb 29 '20 at 06:46
  • @Ch3steR That's not problem in my answer. – Boseong Choi Mar 01 '20 at 16:57
  • If `my_dictionary` was longer than `my_list`, it breaks original keys if reassign dictionary. As I commented, using `my_dictionary.update` can solve that. – Boseong Choi Mar 01 '20 at 16:59
0

Try this :

i = 0
for key in my_dict:
      my_dict[key] = my_list[i]
      i+=1

Output : {'key_a': 'a', 'key_b': 'b', 'key_c': 'c'}

Nishant Agarwal
  • 430
  • 5
  • 17
  • 1
    According to PEP 234, use `my_dict` instead of `my_dict.keys()` – Boseong Choi Feb 29 '20 at 06:11
  • Though the output does not change. I have edited according to your suggestion. – Nishant Agarwal Feb 29 '20 at 06:14
  • Actually, that was the first thing I did! But i thought the "i = 0 ... i += 1" did not smell exactly like Python. My last idea i've come was like use the "reverse()" function to reverse the List and use the "pop()" function to pop each one of the list's element inside the dictionary. But I thought that wasn't smelling good or very readable – Vitor Mafra Feb 29 '20 at 06:17
  • What do you mean : 'does not smell like python'. :D If you want 1 line solution try dictionary comprehensions. – Nishant Agarwal Feb 29 '20 at 06:22
  • @NishantAgarwal sorry If I seemed rude or something bad like that. I was only trying to say that I've thought that was a built-in solution or something more complicated that I did not knew (cause I'm a Python beginner) – Vitor Mafra Feb 29 '20 at 06:39
  • @NishantAgarwal also, thank you very much for your awnser and your tips. You were very helpful :) – Vitor Mafra Feb 29 '20 at 06:40
  • Glad you got what you were looking for. – Nishant Agarwal Feb 29 '20 at 06:41
0

You can try this:

my_list = ['a', 'b', 'c']

my_dict = {
    'key_a' : '',
    'key_b' : '',
    'key_c' : '',
}

for index, key in enumerate(my_dict):
    my_dict[key] = my_list[index]

print(my_dict)
Errol
  • 600
  • 4
  • 16