Computer Science, asked by ssim, 9 months ago

Write a python program to display unique and duplicate items of a given list.[2,7,1,4,9,5,1,4,3] (pls write the PROGRAM )

Answers

Answered by Anonymous
4

Answer:

What is the python program for finding duplicate element from a list?

One thing be careful about indentation.This the most disturbing part about python.This code will not work and will encounter with error if indent it wrongly……..

code 1:

def Print(BKD_list1):

BKD_final_list = []

for num in range(len(BKD_list1)):

i = num + 1

for i in range(i, len(BKD_list1)):

if BKD_list1[num]== BKD_list1[i]:

BKD_final_list.append(BKD_list1[num])

return BKD_final_list

BKD_list1 = [32, 84, 90, 97,85, 88, 97, 84]

print(Print(BKD_list1))

you can also try this code:—(In my opinion this is best because you can print both the unique and duplicate elements in the list here list c contains unique element and d contains the duplicates)

code 2:—-

def duplishow(alist):

c=[]

d=[]

for i in alist:

if i not in c:

c.append(i)

else:

d.append(i)

print("The duplicate element is:",d)

alist=[int(x) for x in input("Enter the list of items").split()]

duplishow(alist)

Answered by sharmaanchal2005
2

Answer:

ill be writing the same program key given in the book with explanation

L1=[2,3,1,4,9,5,1,4,3]

L2=[]

L3=[]

for i in L1

    if i not in L2:

          L2.append(i)

   else:

           L3.append(i)

print(L2)

print(L3)

Explanation:

so first i created the given list and the one empty list (L2) for unique items and one empty list (L3) for duplicate items of the list so we took i as a variable representing any one element and gave another condition that if i is not in L2 that is an empty list so it will print all the elements present at once in L2 and else conditions which states if i IN L2 that is the the element is already present in L2 it means that th element is occuring again therefore we displayed those elements in L3 using append and print the created lists.

Similar questions