'How to delete dictionary key-value pairs if a substring is found at the end of more than one key?
Suppose I have the following dictionary:
dictionary = {
'thegreenmile': '1999',
'scarface': '1983',
'fightclub': '1999',
'thebluemile': '1776',
'theyellowmile': '1893'
}
I want Python to look through the dictionary. If there are keys that end in identical ways, remove the key-value pairs. In the dictionary above, that means while the key-value pair associated with the key thegreenmile remains, the other two key-value pairs associated with the keys thebluemile and theyellowmile are deleted.
You might ask, "What about the beginning? I see the word the duplicated." I'm only focusing on the end, and I haven't been successful in finding something that would allow me to target only the end (I do know about .strip(), but that's very different.)
How can I make it so that Python keeps only one key-value pair if it happens that multiple keys share the same ending? I'm thinking of a threshold of four identical characters at the end.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
