Matt.
Matt.

Reputation: 1294

Insert dictionary key:value into existing dictionary at a specific index in a list

I need a little assistance with inserting a key:value pair from a dictionary (called umDict) into an already existing dictionary in a list at a specific spot in that list.

What I have already is a list (called rLu) that's filled with 943 empty dictionaries. I also have a list called lineList that has parsed a string and contains three elements. Finally, I have a key:value pair in the umDict temporary dictionary that needs to be inserted into the dictionary that's present in the rLu list at
rLu[int(lineList[0])-1]

The key value pair is composed such as:
umDict[lineList[1]] = lineList[2]

To get the results I'd like, I've tried:
umDict[lineList[1]] = lineList[2]
rLu[int(lineList[0])-1] = umDict

But it inserts a brand new dictionary instead of just the key:value into the existing dictionary. How do I get it to insert just the key:value pair into the existing dictionary?

What I'm expecting to get is something like this:
rLu = [{'1':'a','2':'b'}, {'3':'c', '4':'d'}, {'5':'e', '6':'f'}]
Where the length of those dictionaries could be any, not just two like in my example.

Upvotes: 0

Views: 665

Answers (1)

BrenBarn
BrenBarn

Reputation: 251373

Perhaps you want rLu[int(lineList[0])-1].update(umDict)? It's a little difficult to understand your data structure from your description. If I understand you right, you do not have a "key-value pair", you have a one-key dictionary.

Upvotes: 5

Related Questions