search
HomeBackend DevelopmentPython TutorialPython program to remove duplicate elements from array

Python program to remove duplicate elements from array

Sep 07, 2023 am 11:13 AM
pythonarraydelete

Python program to remove duplicate elements from array

An array is a collection of elements of the same data type. Each element in the array is identified by an index value. It is one of the simplest data structures in which each data element can be accessed directly using only its index number.

Arrays in Python

Python has no specific data structure to represent arrays. Here we can use List an array.

 [6, 4, 1, 5, 9]
  0  1  2  3  4

Indices in Python start from 0. In the above code block, the integers 6,4,1,5,9 are array elements and 0,1,2,3,4 are their respective index values.

Arrays can have duplicate elements. In this article, we will discuss several ways to remove duplicate elements from an array.

Input and output scenarios

Suppose we have an input array containing duplicate values. And the resulting array will contain only unique elements.

Input array:
A = [1, 5, 3, 6, 3, 5, 6, 1]
Output array:
[1, 5, 3, 6]

Elements 1, 5, 3, 6 are the only elements in the given array.

Use For Loop

We will use a for loop to iterate over all the array elements and in each iteration we will use the not in operator to find duplicates.

Example

In this example, first we initialize an empty list result to store all the unique values ​​found in the for loop.

lst = [1, 5, 3, 6, 3, 5, 6,  1] 
print ("The original array is: ",lst) 

# Remove repeated elements from array 
result = []

for i in lst: 
   if i not in result: 
      result.append(i) 

print ("The array after removing repeated elements: ", result)

Output

The original array is:  [1, 5, 3, 6, 3, 5, 6, 1]
The array after removing repeated elements:  [1, 5, 3, 6]

The "not in" operator is checking whether the current element exists in an empty list. If it does not exist, the element is appended to the result list, otherwise it is ignored.

Use Collection

Set is a data structure in Python that stores unique data. This means, it does not allow storing duplicate elements.

Example

In this example, we will simply convert the array from a list data type to a collection data type.

lst = [1, 5, 3, 6, 3, 5, 6,  1] 
print ("The original array is: ",lst) 

# Remove repeated elements from array 
result = list(set(lst)) 

print ("The array after removing repeated elements: ", result) 

Output

The original array is:  [1, 5, 3, 6, 3, 5, 6, 1]
The array after removing repeated elements:  [1, 3, 5, 6]

As we all know, duplicates cannot be accommodated in a collection data structure, so we get an output array containing all unique elements.

Use Enumerate() function

Enumerate() is a Python built-in function that accepts an iterable object and returns a tuple containing the count and values ​​obtained by iterating the iterable object.

grammar

enumerate(iterable, start=0)

Example

We will implement the enumerate() function in the list comprehension to keep track of the index of each element in the array, and then we can use the index value i to check whether element n is already present in the array up to index i. If it exists, we ignore the element, otherwise we add it to the resulting array.

lst = [1, 5, 3, 6, 3, 5, 6,  1] 
print ("The original array is: ",lst) 

# Remove repeated elements from array 
result = [i for i, n in enumerate(lst) if n not in lst[:i]]

print ("The array after removing repeated elements: ", result) 

Output

The original array is:  [1, 5, 3, 6, 3, 5, 6, 1]
The array after removing repeated elements:  [1, 5, 3, 6]

Use Dict.fromkeys()

python dict.fromkeys() method is used to create a dictionary based on the given set of keys and values. Dictionaries store a unique set of keys.

grammar

dict.fromkeys(keys, values)

parameter

  • Keys - This is a required parameter. It takes an iteration to specify the keys of the new dictionary.

  • Values - It is an optional parameter, the values ​​of all keys. The default value is "None".

Example

In this example, we will create a dictionary containing only keys, not key and value pairs.

lst = [1, 5, 3, 6, 3, 5, 6,  1] 
print ("The original array is: ",lst) 

# Remove repeated elements from array
 
result = list(dict.fromkeys(lst))

print ("The array after removing repeated elements: ", result) 

Output

The original array is:  [1, 5, 3, 6, 3, 5, 6, 1]
The array after removing repeated elements:  [1, 5, 3, 6]

As we all know, the keys in the dictionary cannot be repeated. Therefore, the fromkeys() method removes duplicate values ​​on its own. Then we convert it to a list to get an array containing all unique elements.

These are some of the methods by which we can remove duplicate elements from an array.

The above is the detailed content of Python program to remove duplicate elements from array. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Python: Is it Truly Interpreted? Debunking the MythsPython: Is it Truly Interpreted? Debunking the MythsMay 12, 2025 am 12:05 AM

Pythonisnotpurelyinterpreted;itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1)Pythoncompilessourcecodeintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).2)Thisprocessallowsforrapiddevelopmentbutcanimpactperformance,req

Python concatenate lists with same elementPython concatenate lists with same elementMay 11, 2025 am 12:08 AM

ToconcatenatelistsinPythonwiththesameelements,use:1)the operatortokeepduplicates,2)asettoremoveduplicates,or3)listcomprehensionforcontroloverduplicates,eachmethodhasdifferentperformanceandorderimplications.

Interpreted vs Compiled Languages: Python's PlaceInterpreted vs Compiled Languages: Python's PlaceMay 11, 2025 am 12:07 AM

Pythonisaninterpretedlanguage,offeringeaseofuseandflexibilitybutfacingperformancelimitationsincriticalapplications.1)InterpretedlanguageslikePythonexecuteline-by-line,allowingimmediatefeedbackandrapidprototyping.2)CompiledlanguageslikeC/C transformt

For and While loops: when do you use each in python?For and While loops: when do you use each in python?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools