0

I have dictionary with values as lists:

    myDict =  {'id1': ["name1","Type1","Value_1"],
    'id2': ["name2","Type1","Value_2"],
    'id3': ["name1","Type2","Value_3"],
    'id4': ["name1","Type1","Value_4"]
    }

I wanna iterate through dictionary and look if Name and Type pair is already in list - replace "Type 1" value by any other and resulting dictionary will be:

    myDict =  {'id1': ["name1","Type1","Value_1"],
    'id2': ["name2","Type1","Value_2"],
    'id3': ["name1","Type2","Value_3"],
    'id4': ["name1","Modified_Type 1","Value_4"]
    }

Have no idea currently how to deal with it with Python

It's mostly question about comparing value[0], value[1] and if both are the same in some other list - replace it.

I'm trying to iterate through existing dictionary and compare if it's values aren't in newDictionary, but obviously I'm checking if those values separately exist in newDict values not as pair:

    myDict =  {'id1': ["name1","Type1","Value_1"],
    'id2': ["name2","Type1","Value_2"],
    'id3': ["name1","Type2","Value_3"],
    'id4': ["name1","Type1","Value_4"]
    }
    newDict = {}
    for key, value in myDict.items():
        if value[0] not in newDict.values() and value[1] not in newDict.values():
    newDict[key] = value
    else:
        newDict[key] = [value[0],"Some modified value",value[2]]
  print (newDict)
  • 3
    In what order? Dictionaries have no ordering, is it okay to just take the current ['arbitrary' ordering](https://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary)? What if the (name, type) pair is repeated again, how should additional pairs be modified to remain unique? – Martijn Pieters Aug 30 '18 at 21:42
  • For Martijn's last question, consider how complicated the rules are for both macOS Finder and Windows Explorer to make sure that duplicating a file repeatedly gets things like `Copy (3) of Spam.txt` instead of `Copy of Copy of Copy of Copy of Spam.txt` (while making sure that if `Copy (3) of Spam.txt` already exists you skip to `Copy (4) of Spam.txt` instead of overwriting it, etc.), and how annoying people still usually find the results. You say "any other", but would you be happy with, say, prepending or appending a UUID or something ugly like that? – abarnert Aug 30 '18 at 21:48
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](http://stackoverflow.com/help/on-topic), [how to ask](http://stackoverflow.com/help/how-to-ask), and [... the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. – Prune Aug 30 '18 at 21:51
  • Why is this information in a dict to begin with? You seem to be using it in table fashion. Where is your coding attempt? You should be able to iterate through the elements and check the value for something. If you have trouble with that on your own, there are plenty of tutorials on line and SO answers that deal with the individual techniques. – Prune Aug 30 '18 at 21:53
  • Order no matters. I just wanna look through dictionary values and if 1)Name and 2) Type are the same in other list, so I'd replace Type for some another value. It's mostly question about comparing value[0], value[1] and if both are the same in some other list – Roman Holovanov Aug 30 '18 at 21:54
  • I refined my answer to match your updated question. – nbwoodward Aug 31 '18 at 00:03
  • Working as I expected. Thanks for interesting solution -will take it to my pure coding arsenal – Roman Holovanov Aug 31 '18 at 08:13
  • Welcome to Stackoverflow, if you accept an answer as correct, you should click the checkmark on the left so the question is resolved. – nbwoodward Aug 31 '18 at 15:06

1 Answers1

1

It's unclear exactly what you want because your result contains Type1 twice... But here's a way to start down the right path.

It sounds like you want your id's to be sorted. So you can get a sorted list of keys like this:

keys = sorted(myDict) #thanks @abarnert

Then iterate through and check for Types:

existingTypes = []
for key in keys:
    theType = myDict[key][1]
    if theType in existingTypes:
        myDict[key][1] = "Modified_" + theType
    else:
        existingTypes.push(theType)

EDIT - Update for your updated question:

This isn't the cleanest maybe, but it will work:

myDict =  {'id1': ["name1","Type1","Value_1"],
'id2': ["name2","Type1","Value_2"],
'id3': ["name1","Type2","Value_3"],
'id4': ["name1","Type1","Value_4"]
}

newDict = {}
for key in sorted(myDict):
    value = myDict[key]
    valuesExist = False

    for newValue in newDict.values():
        if value[0] == newValue[0]  and value[1] == newValue[1]:
            valuesExist = True

    if not valuesExist:
        newDict[key] = value
    else:
        newDict[key] = [value[0],"Some modified value",value[2]]

print (newDict)
nbwoodward
  • 2,816
  • 1
  • 16
  • 24
  • 1
    That `keys()` is only a list if you're on Python 2; on Python 3, it's a `dict_keys` view, and you can't call `sort` on that. Just do `keys = sorted(myDict)` and it'll work on all Python versions, as well as being simpler. – abarnert Aug 30 '18 at 21:53
  • Updated my question to clarify what exactly I'm trying to deal with – Roman Holovanov Aug 30 '18 at 22:22