0% found this document useful (0 votes)
37 views2 pages

Duplicate Elements in Python Lists

The document presents three different programs to identify duplicate elements in a list using various approaches: a brute force method, the Counter function from the collections module, and a single for loop method. Each program is demonstrated with example lists and outputs showing the duplicate elements found. The conclusion confirms the successful study of these methods for duplicating list elements.

Uploaded by

Sanika Bhayde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

Duplicate Elements in Python Lists

The document presents three different programs to identify duplicate elements in a list using various approaches: a brute force method, the Counter function from the collections module, and a single for loop method. Each program is demonstrated with example lists and outputs showing the duplicate elements found. The conclusion confirms the successful study of these methods for duplicating list elements.

Uploaded by

Sanika Bhayde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment no:- 8

Aim:- edit/compile/run a program to duplicate all the element of the list

Program A:- Brute force approach

def duplicate(li):
n=len(li)
dup=[]
for i in range(n):
k=i+1
for j in range(k,n):
if li[i]== li[j] and li[i] not in dup:
[Link](li[i])
return dup
#test
li=["sai","sanika","2004","sanika","2004"]
print("Duplicate element:",duplicate(li))

Output:

Program B:-Counter()function

from collections import Counter


li=["India","Russia","Canada","Mexico","India","Russia"]
d=Counter(li)
repeated_list = list([num for num in d if d[num]>1])
print("Duplicate countries:",repeated_list)

Output:
Program c:- Using a single for loop

lis=["Rose","lily","Sunflower","Rose","lily","Sunflower","Lotus","marigold","Aster","Lotus","Olender"
,"Aster"]
uniqueList = []
duplicateList = []
for i in lis:
if i not in uniqueList:
[Link](i)
elif i not in duplicateList:
[Link](i)
print("Duplicate Flower:",duplicateList)

Output:

Conclusion :- Hence, we have successfully studied about program for duplicating all the
elements of a list

Practical Write up and oral Attendance Total


performance

You might also like