justanewb
justanewb

Reputation: 133

Removing items in a python dictionary under a condition

Suppose I have this dictionary

{0: ['replaced scanner user properly working cage replaced wire damaged stored later use',
  'replaced scanner'],
 1: ['scanner replaced station working well'],
 2: ['station scanner synced base station working'],
 3: ['printer offlineswitched usb portprinter online working'],
 4: ['replaced barcode label reflecting tcs ip address'],
 5: ['restarted thin client using ssh run restart command'],
 6: ['printer reconfigured test functioning normally', 'printer reconfigured'],
 7: ['removed old printer service installed replacement tested good'],
 8: ['tc required reboot rebooted tc aa signin dp functional'],
 9: ['resetting printer factory settings reconfigure'],
 10: ['updated windows forced update laptop operated normally'],
 11: ['printer settings set correct printer working correctly'],
 12: ['power printer disconnected reconnected working fine'],
 13: ['power cycled equipment restocked spooler plastic bubbles'],
 14: ['laptop checked ive logged paskiplacowepl without problem'],
 15: ['reseated scanner cables connection usb port resolve issue'],
 16: ['red scanner reconfigured base rebooted via usb joint'],
 17: ['tested printer station connected working resolved'],
 18: ['reconfigured printer media print properly'],
 19: ['replaced defective device fresh imaged laptop'],
 20: ['reconfigured scanner base tested aa fine'],
 21: ['printer unplugged usb port working properly'],
 22: ['reimaging laptop corrected anyconnect software issue'],
 23: ['reconfigured scanner confirmed scans intended'],
 24: ['reconnected usb cable reassign printer ports port'],
 25: ['plugged usb cable port scanner works'],
 26: ['cleared linux print queue working'],
 27: ['user reset password successfully closing tt'],
 28: ['reset printer get print'],
 29: ['reconfigured hand scanner linked station'],
 30: ['replaced scanner station functional', 'replaced scanner'],
 31: ['laptops battery needed reset asset serial'],
 32: ['printer reconfigured'],
 33: ['upgraded rebooted station'],
 34: ['deploying replacement scanner'],
 35: ['replaced scanner'],
 36: ['updated pc'],
 37: ['tc reimage']}

You can see that the key 0 and 35 have the same string. I would like to have 35 removed. How would I generalize this process in the best pythonic way?

Upvotes: 0

Views: 58

Answers (3)

Joe Ferndz
Joe Ferndz

Reputation: 8508

You can create a list of all the values and check if the value already exits in the list. If yes, then remove it from the values list. Replace the original list with the values. If you find an empty list, then store the key so you can pop it out from the dictionary after you finish iterating over it.

#dict_vals has the original list.

consolidated_vals = [] # to store all values from the list
popkeys = [] #to store keys that need to be deleted

#iterate thru the full dictionary
for k,vals in dict_vals.items():
    
    #check if each value in the values list was already in the list. if found, ignore
    x = [v for v in vals if v not in consolidated_vals]
   
    #if list is empty (all values were already found in the list earler), then store key to pop
    if x:
        consolidated_vals += x  #store new values to the consolidated values list
        dict_vals[k] = x  #replace old values with new values as there may have been a value removed
    else:
        popkeys.append(k)  #store key to pop after iterating thru full list

#now pop all the keys that had 100% match for values already found

for pkeys in popkeys: dict_vals.pop(pkeys)

print (dict_vals)

The output of this will be:

{0: ['replaced scanner user properly working cage replaced wire damaged stored later use', 'replaced scanner'], 
 1: ['scanner replaced station working well'], 
 2: ['station scanner synced base station working'], 
 3: ['printer offlineswitched usb portprinter online working'], 
 4: ['replaced barcode label reflecting tcs ip address'], 
 5: ['restarted thin client using ssh run restart command'], 
 6: ['printer reconfigured test functioning normally', 'printer reconfigured'], 
 7: ['removed old printer service installed replacement tested good'], 
 8: ['tc required reboot rebooted tc aa signin dp functional'], 
 9: ['resetting printer factory settings reconfigure'], 
10: ['updated windows forced update laptop operated normally'], 
11: ['printer settings set correct printer working correctly'], 
12: ['power printer disconnected reconnected working fine'], 
13: ['power cycled equipment restocked spooler plastic bubbles'], 
14: ['laptop checked ive logged paskiplacowepl without problem'], 
15: ['reseated scanner cables connection usb port resolve issue'], 
16: ['red scanner reconfigured base rebooted via usb joint'], 
17: ['tested printer station connected working resolved'], 
18: ['reconfigured printer media print properly'], 
19: ['replaced defective device fresh imaged laptop'], 
20: ['reconfigured scanner base tested aa fine'], 
21: ['printer unplugged usb port working properly'], 
22: ['reimaging laptop corrected anyconnect software issue'], 
23: ['reconfigured scanner confirmed scans intended'], 
24: ['reconnected usb cable reassign printer ports port'], 
25: ['plugged usb cable port scanner works'], 
26: ['cleared linux print queue working'], 
27: ['user reset password successfully closing tt'], 
28: ['reset printer get print'], 
29: ['reconfigured hand scanner linked station'], 
30: ['replaced scanner station functional'], 
31: ['laptops battery needed reset asset serial'], 
33: ['upgraded rebooted station'], 
34: ['deploying replacement scanner'], 
36: ['updated pc'], 
37: ['tc reimage']}

Note here that keys 32 and 35 were removed.

Upvotes: 1

Cute Panda
Cute Panda

Reputation: 1498

You can use the issubset method to find if there are any duplicates repeating in the list and then store the key values in a toBeRemoved list which have to be deleted.

dict1 = {0: ['replaced scanner user properly working cage replaced wire damaged stored later use',
  'replaced scanner'],
 1: ['scanner replaced station working well'],
 2: ['station scanner synced base station working'],
 3: ['printer offlineswitched usb portprinter online working'],
 4: ['replaced barcode label reflecting tcs ip address'],
 5: ['restarted thin client using ssh run restart command'],
 6: ['printer reconfigured test functioning normally', 'printer reconfigured'],
 7: ['removed old printer service installed replacement tested good'],
 8: ['tc required reboot rebooted tc aa signin dp functional'],
 9: ['resetting printer factory settings reconfigure'],
 10: ['updated windows forced update laptop operated normally'],
 11: ['printer settings set correct printer working correctly'],
 12: ['power printer disconnected reconnected working fine'],
 13: ['power cycled equipment restocked spooler plastic bubbles'],
 14: ['laptop checked ive logged paskiplacowepl without problem'],
 15: ['reseated scanner cables connection usb port resolve issue'],
 16: ['red scanner reconfigured base rebooted via usb joint'],
 17: ['tested printer station connected working resolved'],
 18: ['reconfigured printer media print properly'],
 19: ['replaced defective device fresh imaged laptop'],
 20: ['reconfigured scanner base tested aa fine'],
 21: ['printer unplugged usb port working properly'],
 22: ['reimaging laptop corrected anyconnect software issue'],
 23: ['reconfigured scanner confirmed scans intended'],
 24: ['reconnected usb cable reassign printer ports port'],
 25: ['plugged usb cable port scanner works'],
 26: ['cleared linux print queue working'],
 27: ['user reset password successfully closing tt'],
 28: ['reset printer get print'],
 29: ['reconfigured hand scanner linked station'],
 30: ['replaced scanner station functional', 'replaced scanner'],
 31: ['laptops battery needed reset asset serial'],
 32: ['printer reconfigured'],
 33: ['upgraded rebooted station'],
 34: ['deploying replacement scanner'],
 35: ['replaced scanner'],
 36: ['updated pc'],
 37: ['tc reimage']}

toBeRemoved=set()
for x in dict1:
    for y in dict1:
        if(x!=y):
            if(set(dict1[x]).issubset(set(dict1[y]))):
                toBeRemoved.add(x)
for item in toBeRemoved:
    del dict1[item]
print(dict1)

Output

{0: ['replaced scanner user properly working cage replaced wire damaged stored later use', 'replaced scanner'], 1: ['scanner replaced station working well'], 2: ['station scanner synced base station working'], 3: ['printer offlineswitched usb portprinter online working'], 4: ['replaced barcode label reflecting tcs ip address'], 5: ['restarted thin client using ssh run restart command'], 6: ['printer reconfigured test functioning normally', 'printer reconfigured'], 7: ['removed old printer service installed replacement tested good'], 8: ['tc required reboot rebooted tc aa signin dp functional'], 9: ['resetting printer factory settings reconfigure'], 10: ['updated windows forced update laptop operated normally'], 11: ['printer settings set correct printer working correctly'], 12: ['power printer disconnected reconnected working fine'], 13: ['power cycled equipment restocked spooler plastic bubbles'], 14: ['laptop checked ive logged paskiplacowepl without problem'], 15: ['reseated scanner cables connection usb port resolve issue'], 16: ['red scanner reconfigured base rebooted via usb joint'], 17: ['tested printer station connected working resolved'], 18: ['reconfigured printer media print properly'], 19: ['replaced defective device fresh imaged laptop'], 20: ['reconfigured scanner base tested aa fine'], 21: ['printer unplugged usb port working properly'], 22: ['reimaging laptop corrected anyconnect software issue'], 23: ['reconfigured scanner confirmed scans intended'], 24: ['reconnected usb cable reassign printer ports port'], 25: ['plugged usb cable port scanner works'], 26: ['cleared linux print queue working'], 27: ['user reset password successfully closing tt'], 28: ['reset printer get print'], 29: ['reconfigured hand scanner linked station'], 30: ['replaced scanner station functional', 'replaced scanner'], 31: ['laptops battery needed reset asset serial'], 33: ['upgraded rebooted station'], 34: ['deploying replacement scanner'], 36: ['updated pc'], 37: ['tc reimage']}

Upvotes: 0

Tyler Gallenbeck
Tyler Gallenbeck

Reputation: 588

Considering you just want to remove identical values, you could do some dict comprehensions like this:

temp = {val : key for key, val in your_dict.items()} 
res = {val : key for key, val in temp.items()}
print(res)

Note that this will remove the entire key:val from the dictionary if the values are the same.

Upvotes: 0

Related Questions