# Python | Get unique values ​​from a list

| |

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] `

## Shop Learn programming in R: courses

\$ Best Python online courses for 2022

\$ Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method