2

Hello guys i have two list in my code like this

other_concords = ['a','b','c']
leamanyi_concords = ['fruit','drink','snack']

temp_dic = {
              'a':['fruit','drink','snack'],
              'b':['fruit','drink','snack'],
              'c':['fruit','drink','snack']
            }

Is it possible to insert items in my temp_dic using a loop and it appears like this when i output temp_dic?

jpp
  • 147,904
  • 31
  • 244
  • 302

2 Answers2

3
temp_dic = {v: list(leamanyi_concords) for v in other_concords}
AGN Gazer
  • 7,569
  • 2
  • 21
  • 43
1

Use dict.fromkeys if you don't mind your dictionary pointing to the same list.

temp_dic = dict.fromkeys(other_concords, leamanyi_concords)

# {'a': ['fruit', 'drink', 'snack'],
#  'b': ['fruit', 'drink', 'snack'],
#  'c': ['fruit', 'drink', 'snack']}
jpp
  • 147,904
  • 31
  • 244
  • 302
  • Generally not a good idea to use `dict.fromkeys` with some mutable arg. See https://stackoverflow.com/questions/15516413/dict-fromkeys-all-point-to-same-list. It has come back to bite me before – Brad Solomon Feb 08 '18 at 01:58
  • @BradSolomon - it really depends. maybe the intention is that the lists should be linked? I did add a disclaimer. – jpp Feb 08 '18 at 02:03