Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

DICTIONARY PROBLEM

f
federica valeau (450 points)
2 5 7
in Programming in Python by (450 points)
Good evening, to solve a part of the hw6 I have to remove some elements from the values of a dictionary, I'll make an example:

dic = { A : (1), B : (1, 2, 3), C : (1, 3, 5), D : (2, 3)}

this is my dictionary and I'd like to have it turned into:

new_dic = { A : (), B : (2, 3), C : (3, 5), D : (2, 3)}. (I removed all the 1s)

How could I do this?

Thank you in advance for your answers!

1 Answer

S
Silktrader (2550 points)
2 6 16
by (2.6k points)
edited by

I am not sure it's a good idea to perform this kind of operation on tuples. Lists of integers as dictionaries values could be more suitable, as you can mutate them during iterations.

There are nonetheless several ways to accomplish what you want:

  • use a dictionary comprehension that iterates dic.items() and creates a new tuple while removing the first value with a slice such as[1:] โ€” unsure whether that's what you want
  • iterate dic.items() and create a tuple from the difference of two sets for each value
  • use a "for" loop on dic.items() and assign a new tuple to each value by setting it dic[key]
  • use filter to keep all values different from "1" while creating a new tuple โ€” tuple(filter((1).__ne__, v))
  • etc.

It's quite challenging to help you out without posting some form of code. I realise I am tempting Prof. Sterbini's "dislike" hammer here ...

I think that the simplest solution is to create a set of values you want to remove:

one = set(1)

... then use a dictionary comprehension to subtract set "one" set from a newly created set, stemming from your dictionary value โ€” an incredibly obscure English description of:

{k: tuple(set(v)-one) for k, v in dic.items()}

This kind of pattern would work when removing multiple items. Sets should maintain the order tuples elements, but I invite you to verify that's the case.

So sorry for the overly complex answer! Again, in this case, I'd advise to use lists, which are meant to be mutated.

f
federica valeau (450 points)
2 5 7
by (450 points)
Thank you very much for your help, though I think I wasn't enough clear in my example. My actual dictionary has as values sets of tuples. A more similar example would be:

dic = { A : {(1, 1)}, B : {(1, 1), (2, 1),  (2, 3)} C : {(1, 4), (1, 1),  (3, 5)} D : {(2, 3)}} and I want to remove all the tuples (1, 1) from it.

dic = { A : {}, B : {(2, 1),  (2, 3)} C : {(1, 4), (3, 5)} D : {(2, 3)}} like this.

Do you still suggest me to change the type of the values? Can a dictionary contain values as empty sets?

Thank you so much again
S
Silktrader (2550 points)
2 6 16
by (2.6k points)

I think sets are rather convenient in the scenario above. You can indeed include "empty" sets, or even "None" as dictionary values.

To achieve the desired result, iterate dic.values() with a "for" loop. You then discard() the (1, 1) tuple. "Discarding", rather than "removing" (1,1) will prevent exceptions from being raised when it's missing.