0
def makeNewDictFromOld(oldDict):
    newDict = {}
    for key,value in oldDict.items():
        if value compared to something else is true:
            newDict[key].append(value)
    return newDict

If I do this I get an error saying KeyError: last key of old dict.

Is there another way to do this?

Gurkmeja101
  • 592
  • 2
  • 9
  • 24
  • There is no 'last' key in a dictionary. Dictionaries are unordered. You get the `KeyError` for the **first** key in the ['arbitrary' dictionary order](https://stackoverflow.com/questions/15479928/why-is-the-order-in-python-dictionaries-and-sets-arbitrary). – Martijn Pieters May 03 '16 at 08:21
  • It also isn't clear what you expect `newDict` to become, for a given `oldDict`. Do you expect the values to be lists? How are values collected per key? Why use lists in the first place when all `key`s are unique anyway? – Martijn Pieters May 03 '16 at 08:26

2 Answers2

2

You are trying to access a key that doesn't yet exist; your newDict is empty.

If you wanted to populate newDict with lists as the values, you need to first set the key to an empty list. Do so with dict.setdefault():

for key,value in oldDict.items():
    if value compared to something else is true:
        newDict.setdefault(key, []).append(value)

This sets key to [] first, unless the key is already present in the dictionary.

However, since all keys in the old dictionary are unique, you may as well just use:

for key,value in oldDict.items():
    if value compared to something else is true:
        newDict[key] = [value]

That is, unless you were entirely confused and just wanted to set the value directly, and not create lists:

for key,value in oldDict.items():
    if value compared to something else is true:
        newDict[key] = value

Last but not least, you could create the new dictionary entirely in a dictionary comprehension:

def makeNewDictFromOld(oldDict):
    return {key: value for key, value in oldDict.items()
            if value compared to something else is true}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I'm not entirely sure what you mean. Both the key and the value are different for every "row" in the dictionary. It's a date as key with a corresponding value to that date. Would I use the `newDict.setdefault(key, []).append(value)` in the for loop and it would populate the rest of the dictionary as well= – Gurkmeja101 May 03 '16 at 08:21
  • @Gurkmeja101: use it instead of the `newDict[key].append(value)` expression, so in the loop. – Martijn Pieters May 03 '16 at 08:21
  • Thanks for the help. And I'm sorry that you had to guess as to what I needed help with. Made a mistake on how dictionaries work. – Gurkmeja101 May 03 '16 at 08:36
1

Just do this instead of append

def makeNewDictFromOld(oldDict):
    newDict = {}
    for key,value in oldDict.items():
        if True:
            newDict[key] = value #assign directly instead of append
    return newDict

dict_ = {"name":"name1","age":35}
print makeNewDictFromOld(dict_)
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
  • @Gurkmeja101: so why were you using `.append()` in the first place? Did you get this confused with lists? It would have helped if you had given us an expected output, then it'd have been clearer from the start and we wouldn't have had to guess here. – Martijn Pieters May 03 '16 at 08:30
  • @MartijnPieters Yes I was entirely sure what to use. Haven't used dictionaries before so I'm sorry for the inconvenience. Appreciate all the help though! – Gurkmeja101 May 03 '16 at 08:35