0
1.7kviews
Write a python to find out the unique elements in the list.

Write a Python function that takes a list and returns a new list with unique elements of the first list

1 Answer
0
72views

Explanation:-

  • Create a variable n which stores the no of elements user wants to add to the list.
  • Iterate for loop n times and take the elements entered from the user and append them to a list l & pass on this list l to function unique.
  • This function has Initialized an empty list x.
  • Iterate over all the elements in the list l check if that element is present in list x if not then append else move on to next element.

Code:-

def unique(l):
    x=[]
    for a in l:
        if a not in x:
            x.append(a)
    return(x)


l=[]
n=int(input("enter the  no of elements:-"))
for i in range(n):
    print("enter element:-")
    l.append(int(input()))
print("The elements the list:-",l)
print("Unique Elements in the list:-",unique(l))

Output

enter image description here

enter image description here

Please log in to add an answer.