Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Remove Duplicate Elements from a List

Python Remove Duplicate Elements from a List

Document Object Reference Manual Python3 Examples

In this chapter we will learn how to remove duplicate elements from a list.

Topics covered:

Example

list_1

=

[

1

,

2

,

1

,

4

,

6

]

print

(

list

(

set

(

list_1

)

)

)

The output of the code above is:


[1, 2, 4, 6]

In the example above we first convert the list to a set and then back to a list. A set cannot contain duplicate elements, so set() removes the duplicates.

If you need to preserve the order of elements in the original list, you can use an auxiliary set to track elements already seen and build a new list.

Example

Use an auxiliary set to remove duplicates while preserving order

def

remove_duplicates

(

lst

)

:

    seen

=

set

(

)

    unique_list

=

[

]

   

for

item

in

lst:

       

if

item

not

in

seen:

            seen.

add

(

item

)

            unique_list.

append

(

item

)

   

return

unique_list

Example

original_list

=

[

1

,

2

,

2

,

3

,

4

,

4

,

5

]

unique_list

=

remove_duplicates

(

original_list

)

print

(

unique_list

)

 

Output: [1, 2, 3, 4, 5]

Use dict.fromkeys()

The dict.fromkeys() method can also remove duplicates while preserving order, because dictionaries preserve insertion order in Python 3.7 and above.

Example

Use dict.fromkeys() to remove duplicates while preserving order

def

remove_duplicates

(

lst

)

:

   

return

list

(

dict

.

fromkeys

(

lst

)

)

Example

original_list

=

[

1

,

2

,

2

,

3

,

4

,

4

,

5

]

unique_list

=

remove_duplicates

(

original_list

)

print

(

unique_list

)

 

Output: [1, 2, 3, 4, 5]

The output of the code above is:


[1, 2, 3, 4, 5]

Use a list comprehension

A list comprehension combined with a conditional check can also remove duplicates.

Example

Use a list comprehension to remove duplicates

def

remove_duplicates

(

lst

)

:

    unique_list

=

[

]

   

[

unique_list.

append

(

item

)

for

item

in

lst

if

item

not

in

unique_list

]

   

return

unique_list

Example

original_list

=

[

1

,

2

,

2

,

3

,

4

,

4

,

5

]

unique_list

=

remove_duplicates

(

original_list

)

print

(

unique_list

)

 

Output: [1, 2, 3, 4, 5]

Remove elements duplicated across two lists

In the following example, elements present in both lists are removed.

Example

list_1

=

[

1

,

2

,

1

,

4

,

6

]

list_2

=

[

7

,

8

,

2

,

1

]

print

(

list

(

set

(

list_1

)

^

set

(

list_2

)

)

)

First, use set() to convert both lists into sets, which removes the duplicate elements from each list.

Then, use the ^ operator to get the symmetric difference of the two lists.

The output of the code above is:


[4, 6, 7, 8]

First, convert the two lists into two sets in order to remove duplicates from each list.

Then, ^ gives the symmetric difference of the two lists (excluding the overlapping elements of the two sets).

Document Object Reference Manual Python3 Examples