write a program that remove duplicte from list?

0 votes
asked Dec 4, 2019 by thota

5 Answers

+1 vote
answered Dec 8, 2019 by (200 points)
a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
    element=int(input("Enter element" + str(x+1) + ":"))
    a.append(element)
b = set()
unique = []
for x in a:
    if x not in b:
        unique.append(x)
        b.add(x)
print("Non-duplicate items:")
print(unique)
commented 6 days ago by (110 points)
b = set()
unique = []
for x in a:
    if x not in b:
        unique.append(x)
        b.add(x)
print("Non-duplicate items:")
print(unique)
print(''hello world'')
0 votes
answered Dec 11, 2019 by anonymous
x=[1,2,2,3,4,5]

x.remove(2)

x

o/p:[1,2,3,4,5]
+1 vote
answered Dec 12, 2019 by Kshitinjay Kumar
l = [1,2,3,4,1,2,3]

s = set(l)
l = s
print(l)
+1 vote
answered Dec 15, 2019 by (2,870 points) 1 flag

The easiest way is to convert the list into a set and back into a list, since values in a set are unique by definition.

Example:

foo = [1,1,2,3,4,4,5]
print('before: ', foo)
foo = list(set(foo))
print('after: ', foo)

–1 vote
answered 6 days ago by (120 points)
# One Line of Code!

num=[1,2,3,4,5,1,2,4,3,5,8]

print("Before: ",num)
num=list(set(num))

print("After: ",num)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...