Issue
This Content is from Stack Overflow. Question asked by coder_noob
I have several dictionary and i want to remove several key 2 and key 4
dict_1 = {('ABD12-GOU14', '4W', 'ASS 4W LINE 4', 80): [4, 5],
('ABD13-GOU14', '10W', 'ASS 4W LINE 5', 43): [2, 5],
('ABD14-GOU14', '11W', 'ASS 4W LINE 6', 90): [3, 5]}
i want like this
dict_1 = {('ABD12-GOU14', 'ASS 4W LINE 4'): [4, 5],
('ABD13-GOU14', 'ASS 4W LINE 5'): [2, 5],
('ABD14-GOU14', 'ASS 4W LINE 6'): [3, 5]}
Solution
A tuple is immutable, so you need to create new ones.
For example with a dict comprehension to create a new dict where each key will be the 1st and 3rd element of the old key.
{(k[0],k[2]):v for k,v in dict_1.items()}
{('ABD12-GOU14', 'ASS 4W LINE 4'): [4, 5],
('ABD13-GOU14', 'ASS 4W LINE 5'): [2, 5],
('ABD14-GOU14', 'ASS 4W LINE 6'): [3, 5]}
This Question was asked in StackOverflow by coder_noob and Answered by Rabinzel It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.