0

I have dictionary in the form of:

dict_one =  { key2: [val1, val2, val3], key2: [val1, val2, val3] }

I want to remove 'val3' from every element in the dictionary. I believe this should be possible via a for loop but I can't seem to get it correct. I've tried the following:

for lst in dict_one:
    dict_one.pop(dict_one[lst][2])

This is intended, I believe, to remove both key and value. It returned a 'keyerror' error message. dict_one.remove() is clearly not going to work as it's meant for lists. It seems like what I'm trying to do should be relatively simple but I seem to be missing something.

(FYI, it seems like if I only wanted to delete a subset of the 'val3' items, I could follow the approach here: Remove element in dictionary of lists)

Adam Gold
  • 27
  • 5

3 Answers3

1

First, to remove an item from a list:

l = [val1, val2, val3]
l.remove(val3)

Now looping through lists in dictionary:

dict_once = {key1: [val1, val2, val3], key2: [val1, val2, va3]}
for inner_list in dict_one.values():
    inner_list.remove(val3)

Or, looping through using keys:

for key in dict_one:
    dict_one[key].remove(val3)
Harvey Pham
  • 623
  • 4
  • 17
  • If the goal is just to remove the element in index `2`, you'd use `del inner_list[2]`, or to remove the final item `del inner_list[-1]`. It would be weird to be filtering out a specific *value* from each `list` (why does every `list` contain the same value?). – ShadowRanger Aug 25 '22 at 00:58
  • I took `I want to remove 'val3' from every element in the dictionary` as filtering based on value, which is not uncommon. The question might open for another interpretation; only the OP can clarify what he really wants. – Harvey Pham Aug 25 '22 at 01:21
  • Val3 is not the same for every key. I want to remove it because I will be using the mutated dictionary as a basis for comparison with some other data which doesn't include val3 (val3 is actually a time value which changes with each key). For the sake of efficiency therefore, it seems to make sense to get rid of it. In short ShadowRanger is correct in his assumptions. – Adam Gold Aug 25 '22 at 01:45
  • @AdamGold So you want to remove element at index 2. In that case `inner_list.pop(2)` or `del inner_list[2]` should work. – Harvey Pham Aug 25 '22 at 02:01
  • Thanks @Harvey Pham. I was also thinking: let's say I removed val2 as well as val3, that leaves me with { key: [val1] }. At that point the list is redundant. Is there an efficient way to 'unlist' the value so the result is { key: val1 }? – Adam Gold Aug 25 '22 at 02:20
  • Absolutely, ```dict_one = { k: v[0] for k,v in dict_one.items() }``` or ```for key in dict_one: dict_one[key] = dict_one[key][0]``` – Harvey Pham Aug 25 '22 at 02:23
  • Thanks for showing both approaches, very helpful. One other question: the dict comprehension approach creates a new dictionary and, as it happens in this case, given the program flow the original version will no longer be useful. As a matter of good practice should I delete the original version to free up memory (in this particular instance it won't be a large amount of memory but I want to understand the right approach). – Adam Gold Aug 25 '22 at 02:29
1

To get a specific item from list of dictionary:

for lst in dict_one:
    print(dict_one.get(lst)[item_num])

to modify an entry:

for lst in dict_one:
    dict_one[lst] = new_value
    print(dict_one.get(lst))

The changes you wanted to your lists:

dict_one =  { 'key2': ['val1', 'val2', 'val3'], 'key3': ['val1', 'val2', 'val3'] }

for lst in dict_one:
    dict_one[lst] = dict_one.get(lst)[:-1]
    print(dict_one.get(lst))
Kineye
  • 154
  • 7
  • Just did. The reason for the delay was I tried an upvote immediately after I read the response and it said I didn't yet have enough reputation points. I assumed that also applied to accepting the answer which, of course, is wrong. – Adam Gold Aug 25 '22 at 22:28
0

One way using dict comprehension:

dict_one = {'key2': ['val1', 'val2', 'val3'], 'key3': ['val1', 'val2', 'val3']}
dict_two = {k: v[:-1] for k, v in dict_one.items()}
print(dict_two)

Output:

{'key2': ['val1', 'val2'], 'key3': ['val1', 'val2']}
Chris
  • 29,127
  • 3
  • 28
  • 51
  • I was hoping there was a dict comprehension approach too. Even though I can see how they basically equate to a for loop, I'm still getting to grips with the syntax. – Adam Gold Aug 25 '22 at 02:18