0

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']
2
  • You made a little typo error in 2nd list. You show "a" as unique and "d" as dup2. Commented May 21, 2021 at 11:45
  • I will do it ASAP Commented May 21, 2021 at 11:46

2 Answers 2

3

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']
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.