Get Unique Elements from List in Python

04-Nov-2022

.

Admin

Get Unique Elements from List in Python

Hi Dev,

This post will give you example of Get Unique Elements from List in Python. This tutorial will give you simple example of Python Only Unique Elements in List. I would like to share with you Python All Unique Elements in List. you can see How to Find Unique Elements in List Python.

There are several ways to get unique values from the list in python. we will use set() and dict() functions to get unique elements from list. so let's see the below examples.

so let's see following examples with output:

Example 1:


main.py

myList = ['one', 'two', 'two', 'three', 'four', 'five', 'five']

# Remove Duplicate Value from List

newList = list(set(myList))

print(newList)

Output:

['three', 'two', 'four', 'five', 'one']

Example 2:

main.py

myList = [1, 2, 3, 4, 4, 5, 5]

# Remove Duplicate Value from List

newList = list(dict.fromkeys(myList))

print(newList)

Output:

[1, 2, 3, 4, 5]

#Python