1

I have the code:

a = {"listA" : ("keyA", "keyB"), "listB" : ("keyC", "keyD")}

How can I for example remove the KeyB so:

a = {"listA" : ("keyA"), "listB" : ("keyC", "keyD")}
0

1 Answer 1

4

You have a dict not a list with tuples as values, you would need to reassign the value as tuples are immutable so you cannot remove an element:

a = {"listA" : ("keyA", "keyB"), "listB" : ("keyC", "keyD")}

a["listA"] =  a["listA"][0],
print(a)

If you want to be able to modify the values use lists as values which are mutable:

a = {"listA" : ["keyA", "keyB"], "listB" : ["keyC", "keyD"]}

a["listA"].remove("keyB")

print(a)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that was quite informing!
@GeorgeTsak, no worries, if you control how the dict is created and want to be able to modify the values easily use a list.

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.