How to remove duplicate word in dictionary (python)
Question:
I have a dictionary called top_topics that has the following words (example):
{0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
I would like to remove football from the dictionary but haven’t been able to figure out a way how. I created a loop that in my mind would fix the issue but it hasn’t:
for key, value in topic_keywords.items():
for item in value:
if item not in topic_keywords.values():
unique_dict[key] = value
topic_keywords = unique_dict
but I still get the same results. What am I doing wrong and how can I fix it?
Answers:
You can enumerate
over the dictionary and use the index to check if the value is not in a slice of the dictionary values and was inserted previously
top_topics = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
values = list(top_topics.values())
new_top_topics = {}
for i, (k, v) in enumerate(top_topics.items()):
if v not in values[:i]:
new_top_topics[k] = v
or check if the value is already in the new dictionary
top_topics = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
new_top_topics = {}
for i, (k, v) in enumerate(top_topics.items()):
if v not in new_top_topics.values():
new_top_topics[k] = v
With dict comprehensions
top_topics = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
values = list(top_topics.values())
top_topics = {k: v for i, (k, v) in enumerate(top_topics.items()) if v not in values[:i]}
Results
{0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball']}
This is a good use-case for a set. You also need to allow for the fact your values (in the original dictionary) are lists.
data = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
new_data = dict()
values = set()
for k, v in data.items():
for _v in v:
if _v not in values:
new_data.setdefault(k, []).append(_v)
values.add(_v)
print(new_data)
Output:
{0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball']}
I have a dictionary called top_topics that has the following words (example):
{0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
I would like to remove football from the dictionary but haven’t been able to figure out a way how. I created a loop that in my mind would fix the issue but it hasn’t:
for key, value in topic_keywords.items():
for item in value:
if item not in topic_keywords.values():
unique_dict[key] = value
topic_keywords = unique_dict
but I still get the same results. What am I doing wrong and how can I fix it?
You can enumerate
over the dictionary and use the index to check if the value is not in a slice of the dictionary values and was inserted previously
top_topics = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
values = list(top_topics.values())
new_top_topics = {}
for i, (k, v) in enumerate(top_topics.items()):
if v not in values[:i]:
new_top_topics[k] = v
or check if the value is already in the new dictionary
top_topics = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
new_top_topics = {}
for i, (k, v) in enumerate(top_topics.items()):
if v not in new_top_topics.values():
new_top_topics[k] = v
With dict comprehensions
top_topics = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
values = list(top_topics.values())
top_topics = {k: v for i, (k, v) in enumerate(top_topics.items()) if v not in values[:i]}
Results
{0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball']}
This is a good use-case for a set. You also need to allow for the fact your values (in the original dictionary) are lists.
data = {0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball'], 4: ['football']}
new_data = dict()
values = set()
for k, v in data.items():
for _v in v:
if _v not in values:
new_data.setdefault(k, []).append(_v)
values.add(_v)
print(new_data)
Output:
{0: ['soccer'], 1: ['football'], 2: ['hockey'], 3: ['basketball']}