Reputation: 21
I have an unordered python list, what I want is to create 2nd list which will tell either the values in the first list are duplicate or unique. For duplicates i have to mark them as duplicate1, duplicate2 and so on. I will create a dictionary from these lists and later on these will be use in pandas dataframe.
I am stuck on logic of 2nd_list, could someone please help.
first_List = ['a', 'b', 'c', 'a', 'd', 'c']
EXPECTED OUTPUT:
second_List = ['dup1', 'unique', 'dup1', 'dup2', 'unique', 'dup2']
Upvotes: 0
Views: 131
Reputation: 151
To be short
first_List = ['a', 'b', 'c', 'a', 'd', 'c']
d = {i:'' for i in first_List if first_List.count(i) > 1}
second_List = ['unique' if i not in d.keys() else f'dup{list(d.keys()).index(i)+1}' for i in first_List]
It works fine.
This is the same as
d = {i:'' for i in first_List if first_List.count(i) > 1}
second_List = list()
for i in first_List:
text = 'unique' if i not in d.keys() else f'dup{list(d.keys()).index(i)+1}'
second_List.append(text)
Upvotes: 0
Reputation: 18416
You can iterate the list by index
, and for list value at given index
, check if it is duplicate or not (isDuplicate
) boolean is created in the code below, if it is a duplicate entry, then count how many times the current value appeared in the list for the given index
and append the string to the second_List
second_List = []
for i in range(len(first_List)):
isDuplicate = first_List.count(first_List[i]) > 1
if isDuplicate:
count = first_List[:i+1].count(first_List[i])
second_List.append(f'dup{count}')
else:
second_List.append('unique')
OUTPUT:
['dup1', 'unique', 'dup1', 'dup2', 'unique', 'dup2']
Here is the equivalent List-Comprehension as well, if you are interested!
>>> [f'dup{first_List[:i+1].count(first_List[i])}'
... if first_List.count(first_List[i]) > 1
... else 'unique'
... for i in range(len(first_List))]
['dup1', 'unique', 'dup1', 'dup2', 'unique', 'dup2']
Upvotes: 3