Hi folks,

Just hoping someone can point me in the right direction with this one.
Say I have these key:value pairs in a dictionary, d1:

d1 = {fruit:['apples', 'bananas','oranges','tomatoes'], vegetables:['carrots','lettuces']}

And I wanted to remove a list (containing tomatoes, oranges) from the value of the key fruit and add those list contents to vegetables.

fruit_veg_list = ['tomatoes','oranges']

Now I've worked out concatenations just fine, so I've got that list adding to "vegetables". That's the easy part.

Trouble is, when trying a del or remove or similar, to get rid of the fruit_veg_list values from the key fruit, I simply can't work out how to do it. This makes sense, since the list itself doesn't exist in fruit's values! Yet I can't work out a way to take those items out of the list and remove them one by one.

Is there a sort of "reverse concatenation" for this situation? I'm a bit stuck but if someone could guide me in the right direction it would be much appreciated.

I'm not quite sure I understand your exact question, but maybe this will help:

If you wanted to remove orange from fruit's dictionary entry try:

del d1['fruit'][2]

to add orange over to vegetables:

d1['vegetables'].append('orange')

To go even further, to get the correct index programmatically so you don't have to just 'know' orange's position in the fruit's list use:

#if fruittoremove exists in the fruit list, find its index
if fruittoremove in d1['fruit']:
    dIndex=d1['fruit'].index(fruittoremove)
    return dIndex

#delete item using the index we just found
del d1['fruit'][dIndex]

#add item to other list
d1['vegetables'].append('fruittoremove')

You can even combine them into single lines of code:

#itemTOmove can be any fruit
if itemTOmove in d1['fruits']:
    del d1['fruits'][d1['fruits'].index(itemTOremove)]
    d1['vegetables'].append(itemTOremove)

Is that enough to get you on the right track?

It is! I've worked out a way to adapt your individual value coding to remove the values specified list (using a for loop). It's working quite well.

Many thanks for your help!

And just for fun, you can turn it into a handy function:

#itemtomove=item you wish to move between lists
#dictname=source dictionary name
#srclist=source list name(one to delete from)
#destlist=destination list (one to append to)

def movelistitem(dictname,itemtomove,,srclist,destlist):
    if itemtomove in dictname[srclist]:
        del dictname[srclist][dictname[srclist].index(itemtomove)]
        dictname[destlist].append(itemtomove)
#usage to move entry "apple" from "fruit" to "vegetable" list, in the dictionary named d1
movelistitem('d1','apple','fruit','vegetables']

If there are any mistakes, please forgive me. Today is my first day using python!

Thanks!

If there are any mistakes, please forgive me. Today is my first day using python!

This is my third week of doing it, and you understood how to do that dictionary/list operation better than I did! Thanks again for a detailed and easy to understand reply.

This question has already been answered. Start a new discussion instead.