Python | Get unique values ​​from a list

get unique values | NumPy | Python Methods and Functions

Examples :

 Input: 10 20 10 30 40 40 Output: 10 20 30 40 Input: 1 2 1 1 3 4 3 3 5 Output: 1 2 3 4 5 

Method 1: traversing the list

Using traversal, we can traverse for each element in the list and check if an item in the unique_list, if it is not there, then we can add it to the unique_list. This is done using one for a for loop and another if statement that checks if a value is in a unique list or not, which is equivalent to another for a loop.

# Python program to check if two
# get unique values ​​from the list
# use bypass

 
# function to get unique values ​​

def unique (list1):

  

# use an empty list

unique_list = []

 

# move for all elements

for x in list1:

# check if unique_list exists or not

if x not in unique_list:

  unique_list.append (x)

# print list

for x in unique_list:

  print x,

 

 

 
# driver code

list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ]

print ( " the unique values ​​from 1st list is " )

unique (list1)

  

 

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

print ( " the unique values ​​from 2nd list is " )

unique (list2)

Exit:

 the unique value s from 1st list is 10 20 30 40 the unique values ​​from 2nd list is 1 2 3 4 5 

Method 2: Using Set

Using the

# Python program to check if two
# get unique values ​​from the list
# using a set

 
# function to get unique values ​​

def unique (list1):

 

# insert list into set

list_set = set (list1)

  # convert set to list

  unique_list = ( list (list_set))

for x in unique_list:

  print x,

 

 
# driver code

list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ]

print ( "the unique values ​​from 1st list is" )

unique (list1)

 

 

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

print ( "the unique values ​​from 2nd list is" )

unique (list2)

Exit :

 the unique values ​​from 1st list is 40 10 20 30 the unique values ​​from 2nd list is 1 2 3 4 5 

Method 3: Using numpy.unique

Using Python import numpy also produces unique elements in an array. First, convert the list to x = numpy.array (list), and then use the numpy.unique (x) function to get unique values ​​from the list.  numpy.unique () only returns unique values ​​in the list.

# Ppython program to check if two
# get unique values ​​from the list
# using numpy.unique

import numpy as np

 
# function for getting unique values ​​

def unique (list1 ):

x = np.array (list1)

print (np.unique (x))

 

 
# driver code

list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ]

print ( "the unique values ​​from 1st list is" )

unique (list1)

  

 

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

print ( "the unique values ​​from 2nd list is" )

unique ( list2)

Exit

 the unique values ​​from 1st list is [10 20 30 40] the unique values ​​from 2nd list is [1 2 3 4 5] 




Python | Get unique values ​​from a list: StackOverflow Questions

Get unique values from a list in python

I want to get the unique values from the following list:

["nowplaying", "PBS", "PBS", "nowplaying", "job", "debate", "thenandnow"]

The output which I require is:

["nowplaying", "PBS", "job", "debate", "thenandnow"]

This code works:

output = []
for x in trends:
    if x not in output:
        output.append(x)
print(output)

is there a better solution I should use?

Get Solution for free from DataCamp guru