Create a List With Duplicate Items in Python

Create A List With Duplicate Items In Python

When we extract information from data, we come across identical values very often. There are ways in which we can eliminate the duplicates to create a unique array. Or we can also determine which elements have a copy and create a list with the duplicate items.

In this tutorial, we will look at how we can identify the elements with more than one occurrence in a list, and then create another new list with these elements.

In python, identifying duplicates in a list is relatively easier than in other languages. It can be either done with built in functions or user defined blocks of code.

What is a python list?

A python list is a data structure that is similar to arrays in other languages. Python lists are extremely useful for data manipulation and retaining information. Python lists are one of the most popular python data types besides sets, tuples and dictionaries. Lists are immutable data types, that is, their values can be explicitly changed whenever and wherever necessary.

Python lists are also used to implement matrices and numerical python arrays for scientific computations. The list datatype has one of the biggest built in function libraries in python. With functions like count() and append() we can easily identify and truncate copies of the same element.

Suggested: Python data types

How to create a list with duplicates

The main issue that we need to resolve is recognize which elements have another copy in a list. This can be done in two ways:

  • Manually writing a block of code that will use for loops to traverse through the elements of a list one by one, and then find duplicates.
  • Or, To implement the above mentioned technique with the help of built in functions.

Let us explore both methods and you can use whatever you see fit for your program.

Method 1: Manually traversing through lists in python

In this method, we have to use for loops to manually traverse through the elements of a list and find a match accordingly. Then we have to check for the occurrence of each element using an if statement. Finally, we are going to append only those elements which occur more than once in a new list called duplicate. Let’s look at the code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# assigning two list object

(lst, duplicate) = ([], [])  # lst is for original and duplicate is for duplicates list

# declaring size of list
N = int(input('Enter the size of list='))

# input for list
for i in range(N):
    ele = input('Enter the ' + str(i + 1) + ' element= ')
    lst.append(ele)

# displaying the original list
print ('The original list is=', lst)

# traversing through the list
for i in range(N):
    check = lst[i]

  # check for duplicates
    for j in range(i + 1, N):
        if check == lst[j]:
            duplicate.append(check)

print ('the list with the elements which have more than one instance in the original=', duplicate)

The first part of the code takes in user input for the list, and then it will identify the duplicates, and then create a list with those elements which have more than one copy in the original list. After running the code and providing the input, we get the following output:

Enter the size of list=5
Enter the 1 element= hi
Enter the 2 element= hi
Enter the 3 element= bye
Enter the 4 element= bye
Enter the 5 element= hello
The original list is= ['hi', 'hi', 'bye', 'bye', 'hello']
the list with the elements which have more than one instance in the original= ['hi', 'bye']
Traversing Through A List And Identify Copies
Traversing Through A List And Identify Copies

Read more: 5 Easy Ways To Extract Elements From A Python List.

Method 2: Using the count() function

The count() function computes the number of instances of a particular element in a set or a list. We can also explicitly convert another datatype into a list, hence in this method that feature also comes in handy.

#assigning two list object
lst,duplicate=[],[] #lst is for original and duplicate is for duplicates list
#declaring size of list
N=int(input("Enter the size of list="))
#input for list
for i in range(N):
    ele=input("Enter the "+str(i+1)+" element= ")
    lst.append(ele)
#displaying the original list
print("The original list is=",lst)
duplicate = list({x for x in lst if lst.count(x) > 1})
print("The list with elements whose duplicates exist=",duplicate)

The output would be:

Enter the size of list=5
Enter the 1 element= hi
Enter the 2 element= yo
Enter the 3 element= yo
Enter the 4 element= bro
Enter the 5 element= no
The original list is= ['hi', 'yo', 'yo', 'bro', 'no']
The list with elements whose duplicates exist= ['yo']
Using Count() Funciton
Using Count() Function

Conclusion

To conclude, in this tutorial we have seen the two ways in which we can identify duplicates in a list. When collecting data from different sources, we have to check for the instances of a single element in a huge array. In the first method, the implementation is done in a more mechanical way whereas in the second illustration we have used the privileges that python presents in the form of built in modules. To know more about python lists, click here.