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,480 points
3,593 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,770 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
797 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,560 points
1,767 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,230 points
881 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
5,240 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 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,790 points
4,071 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,770 points
2,388 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,770 points
799 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP