1

I have a dictionary with multiple values per key and each value has two elements (possibly more). I would like to iterate over each value-pair for each key and delete those values-pairs that meet some criteria. Here for instance I would like to delete the second value pair for the key A; that is: ('Ok!', '0'):

myDict = {'A': [('Yes!', '8'), ('Ok!', '0')], 'B': [('No!', '2')]}

to:

myDict = {'A': [('Yes!', '8')], 'B': [('No!', '2')]}

I can iterate over the dictionary by key or value, but can't figure out how I delete a specific value.

5
  • show us your code Commented Sep 6, 2016 at 13:43
  • Please add your code then it will be easy for us to help Commented Sep 6, 2016 at 13:44
  • 1
    You can use pop. In your example myDict['A'].pop(1) . Commented Sep 6, 2016 at 13:44
  • you can use remove. for example myDict['A'].remove( ('Ok!', '0') ) Commented Sep 6, 2016 at 13:48
  • Thank you! Works like a charm! Commented Sep 6, 2016 at 14:07

3 Answers 3

5

The code like this:

myDict = {'A': [('Yes!', '8'), ('Ok!', '0')], 'B': [('No!', '2')]}
for v in myDict.values():
    if ('Ok!', '0') in v:
        v.remove(('Ok!', '0'))
print(myDict)
Sign up to request clarification or add additional context in comments.

1 Comment

Minor note: If the lists might be long and removals semi-common, it would be better to do try: v.remove(('Ok!', '0')), except ValueError: pass, so you're only iterating the list once, not once to check, and a second time to remove. Also, if you're on Python 2, use .itervalues() or .viewvalues() over .values(), to avoid creating an unnecessary temporary list of the values; on Py2, .values() is equivalent to Py2.7's .viewvalues(), so it avoids the temporaries automatically.
0

You can also do like this:

myDict = {'A': [('Yes!', '8'), ('Ok!', '0')], 'B': [('No!', '2')]}
del myDict['A'][1]
print(myDict)

Explanation :

myDict['A'] - It will grab the Value w.r.t Key A

myDict['A'][1] - It will grab first index tuple

del myDict['A'][1] - now this will delete that tuple

Comments

-3
myDict = {'A': [('Yes!', '8'), ('Ok!', '0')], 'B': [('No!', '2')]}
for v in myDict.values():
    for x in v:
        if x[0] == 'Ok!' and x[1] == '0':
            v.remove(x)
print(myDic)

3 Comments

This is a minor change to the response of @Em L . With this modification, it is possible to check for a condition on the first element of a member of all values.
The element to be found was value pair ('Ok!', '0'). If checking just first element for 'Ok!', also other tuplets like ('Ok!', '1') will match. Therefore, this is not an answer to the question.
@AminBa rather than trying to explain your answer in the comments, please use the edit button under your answer and modify it to include the information that you have put in the comments (and then delete the comments)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.