-1

i have a list that contains a few dictionaries. I want to extract the dictionaries from this list and add them to a new dictionary.

testList = [{"Name":"Jim"},{"Age":"25"},{"Location":"UK"}]

however, i can't quite get it to work. so far, I've tried a simple for loop to sift through the list and update it to a new target accordingly, if the entries found in it happen to be a dictionary:

for dictionary in testList:
  if dictionary is dict:
    for k, v in dictionary.items(): 
        newDict = {}
        newDict.update(k,v)

i managed to get it working, but it seemingly only updated the last dictionary entry in the list and not just add them all to it when i was experimenting with earlier code. I also tried to take a copy of it calling the '.copy()' method but that had no luck either. Now the newDict, when i print it, is empty still! anyone know what i'm doing wrong? i'd appreciate any help here because i have a feeling i'm not too far off! i can print the length of the list and see it has 3 items, which is expected. So i'm not sure why when i had it partially working before that it only lifted the last dictionary only.

  • `newDict = {}` is executed everytime and it re-initialises to empty dictionary. So what you have in the end is what you updated at the last. – Austin Aug 05 '20 at 15:11
  • Do you expect that `testList` contains anything else than dictionaries? If not, you do not need to test if each item is a dictionary, just assume it is. – mkrieger1 Aug 05 '20 at 15:11
  • You forgot to show us the desired result. – superb rain Aug 05 '20 at 15:15
  • You also mean `isinstance(dictionary, dict)`, not `dictionary is dict` (if it is necessary to test this at all). – alani Aug 05 '20 at 15:17
  • And you are also using `update` wrongly. You would just give it a dictionary as argument - no need to loop over the items. – alani Aug 05 '20 at 15:21
  • testList might contain something else down the line for what i'm testing in the future. So i thought it might make sense to test if the entries in it are dictionaries. Sorry by the way guys i'm kinda new to this, lol – Python4Life Aug 05 '20 at 15:21

3 Answers3

0

Here is corrected version of your code:

testList = [{"Name":"Jim"},{"Age":"25"},{"Location":"UK"}]

newDict = {}
for dictionary in testList:
  if isinstance(dictionary, dict):
    newDict.update(dictionary)

print(newDict)

Points to note:

  • initialise newDict outside the loop

  • isinstance to test if dictionary is a dict; dictionary is dict would test if it is the same object as the type dict - this will almost certainly evaluate False. (And you only need to do the test if there is some possibility that it might not be a dictionary.)

  • the argument to update is a dictionary. It will copy all the items across, and there is no need to loop over the items explicitly.

alani
  • 12,573
  • 2
  • 13
  • 23
0

I'm quite new to programming and Python but here's my idea.

testList = [{"Name":"Jim"},{"Age":"25"},{"Location":"UK"}]

newDict = {}
for dictionary in testList:
    newDict.update(dictionary)

print(newDict)

What I think caused the problem was that you put the dictionary inside the for loop and it should be outside. I hope it helped and feel free to correct my solution.

VML13
  • 1
  • 1
-1
  1. You can't check in this manner: dictionary is dict. Instead of it you check if the type is dict or not like type(dictionary) is dict
  2. you are creating new dictionary for every element in the list and this is replacing the old values.
  3. The syntax of update is newDict.update({k:v}) or you can simply write like this newDict[k] = v.

You can modify your code like this.

testList = [{"Name":"Jim"},{"Age":"25"},{"Location":"UK"}]

newDict = {}

for dictionary in testList:
  if type(dictionary) is dict:
    for k, v in dictionary.items(): 
        newDict.update({k: v})

print(newDict)
Dinesh
  • 812
  • 4
  • 14