How to produce a list with unique elements from a list with duplicate elements

0 votes

Hello,

This question has been asked to me in my interview, please can anyone explain how to produce a list with unique elements from a list with duplicate elements?

Jun 26, 2020 in Python by Roshni
• 10,420 points
603 views

1 answer to this question.

0 votes

Hey, @Roshni,

Iterating the list is not a desirable solution. The right answer should look like this. You can go through this example:

duplicates = ['a','b','c','d','d','d','e','a','b','f','g','g','h']
uniqueItems = list(set(duplicates))
print sorted(uniqueItems)

Output

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
answered Jun 26, 2020 by Gitika
• 65,870 points

Related Questions In Python

0 votes
0 answers

How to remove elements from a list that may or may not be present?

In case I want to remove some ...READ MORE

Jun 27, 2019 in Python by Nisa
• 1,090 points
163 views
0 votes
1 answer

How to zip with a list output in Python instead of a tuple output?

Good question - Considering that you are ...READ MORE

answered Feb 7, 2019 in Python by Nymeria
• 3,520 points
426 views
0 votes
1 answer

How to remove the duplicate values in a list?

a = [10,20,30,40,10,20,30,40,30,40,50,60] s = set(a) c = list(s) print(c) this ...READ MORE

answered Mar 20, 2019 in Python by Mohammad
• 3,210 points
108 views
0 votes
1 answer

How to count the number of elements in a list?

To count the number of elements of ...READ MORE

answered May 27, 2019 in Python by Nisa
• 1,090 points
214 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,842 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,419 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,870 points
577 views
0 votes
1 answer

How to remove the last object from a list?

Hi, @Roshni, Try running the following piece of ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,870 points
89 views