# Extracting unique values from a list or an array - Python
Data Science for Political and Social Phenomena
Author: Guillaume Redoulès

# Extracting unique values from a list or an array

## Using lists

An easy way to extract the unique values of a list in python is to convert the list to a set. A set is an unordered collection of items. Every element is unique (no duplicates) and must be immutable.

    my_list = [10, 20, 30, 40, 20, 50, 60, 40]
    print(f"Original List : {my_list}")

    my_set = set(my_list)
    my_new_list = list(my_set) # the set is converted back to a list with the list() function
    print(f"List of unique numbers : {my_new_list}")

    Original List : [10, 20, 30, 40, 20, 50, 60, 40]
    List of unique numbers : [40, 10, 50, 20, 60, 30]

## Using numpy

If you are using numpy you can extract the unique values of an array with the unique function builtin numpy:

    import numpy as np
    arr = np.array(my_list)
    print(f'Initial numpy array : {arr}\n')

    unique_arr = np.unique(arr)
    print(f'Numpy array with unique values : {unique_arr}')

    Initial numpy array : [10 20 30 40 20 50 60 40]

    Numpy array with unique values : [10 20 30 40 50 60]

### Find an error or bug? Have a suggestion?

Everything on this site is avaliable on GitHub. Head on over and submit an issue. You can also message me directly by email.