Python program to remove duplicate elements from array

王林
Release: 2023-09-07 11:13:02
forward
1805 people have browsed it

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
Copy after login

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]
Copy after login

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)
Copy after login

Output

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

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) 
Copy after login

Output

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

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)
Copy after login

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) 
Copy after login

Output

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

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)
Copy after login

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) 
Copy after login

Output

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

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!