Change language

# Python | Check if the list contains all unique items

|

Method # 1: The Naive Method
Solutions usually start with the simplest method that can be applied to a specific task. Here you can use a nested loop to check if a similar element exists after this element in the remaining list.

 ` # Python3 demo code ` ` # to check all items in the list are unique ` ` # using a naive method `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 3 ` `, ` ` 4 ` ` , ` ` 6 ` `, ` ` 7 ` `] ` ` `  ` # print original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` flag ` ` = ` ` 0 `   ` # using the naive method ` ` # check all unique list items ` ` for ` ` i ` ` in ` ` range ` ` (` ` len ` ` (test_list)): ` ` for ` ` i1 ` ` i n ` ` range ` ` (` ` len ` ` (test_list)): ` ` ` ` if ` ` i! ` ` = ` ` i1: ` ` ` ` if ` ` test_list [i] ` ` = ` ` = ` ` test_list [i1]: ` ` flag ` ` = ` ` 1 `   ` `  ` # print result ` ` if ` ` (` ` not ` ` flag ): ` `  print ( "List contains all unique elements" ) `` else :  print ( "List contains does not contain all unique elements " ) `

Output:

` The original list is: [1, 3, 4, 6, 7] List contains all unique elements `

Method # 2: Using ` len () + set () `
This is the most elegant way to solve this problem with one line. This solution converts the list to install and then tests with the original list if it contains a similar number. elements.

 ` # Python3 demo code ` ` # to check all items in the list are unique ` ` # using set () + len () `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 3 ` `, ` ` 4 ` `, ` ` 6 ` `, ` ` 7 ` `] `   ` # print original list ` ` print ` ` (` ` "The original list is:" ` ` + ` ` str ` ` (test_list)) `   ` flag ` ` = ` ` 0 ` ` `  ` # using set () + len () ` ` # check all unique list items ` ` flag ` ` = ` ` len ` ` (` ` set ` ` (test_list)) ` ` = ` ` = ` ` len ` ` (test_list) `     ` # print result ` < code class = "keyword"> if ` (flag): ` ` print ` ` (` ` "List contains all unique elements" ` `) ` ` else ` `: ` ` print ` ` (` ` "List contains does not contain all unique elements "` `) `

Output:

` The original list is: [1, 3, 4, 6, 7] List contains all unique elements `

Method # 3: Using `Counter.itervalues()`
This is one of various methods that uses the frequency obtained for all elements using a counter and checks , whether any of the frequencies exceeds 1.

Output:

` The original list is: [1, 3, 4, 6, 7] List contains all unique elements `

## 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

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

 ` # Python code for demo ` ` # to check all items in the list are unique ` ` # using Counter.itervalues ​​() ` ` from ` ` collections ` ` import ` ` Counter `   ` # initializing list ` ` test_list ` ` = ` ` [` ` 1 ` `, ` ` 3 ` `, ` ` 4 ` `, ` ` 6 ` `, ` ` 7 ` `] ` ` `  ` # print the original list ` ` print ` ` (` `" The original list is: "` ` + ` ` str ` ` (test_list)) `   ` flag ` ` = ` ` 0 ` ` `  ` # using Counter.itervalues ​​() ` ` # check all unique list items ` ` counter ` ` = ` ` Counter (test_list) ` ` for ` ` values ​​` ` in ` ` counter.itervalues ​​(): ` ` ` ` if ` values ​​& gt;  ` 1 ` `: ` ` flag ` ` = ` ` 1 `     ` # print result ` ` if ` ` (` ` not ` ` flag): ` ` ` ` print ` ` (` ` "List contains all unique elements" ` `) ` ` else ` `: ` ` ` ` print ` ` (` ` "List contains does not contains all unique elements" ` `) `