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
  • 159,742
  • 34
  • 281
  • 339
MoffatMore
  • 67
  • 1
  • 8

2 Answers2

3
temp_dic = {v: list(leamanyi_concords) for v in other_concords}
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
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
  • 159,742
  • 34
  • 281
  • 339
  • 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