w3resource

Python: Check if the elements of a given list are unique or not

Python List: Exercise - 115 with Solution

Write a Python program to check if the elements of a given list are unique or not.

Sample Solution-1:

Python Code:

def all_unique(test_list):
    if len(test_list) > len(set(test_list)):
        return False
    return True

nums1 = [1,2,4,6,8,2,1,4,10,12,14,12,16,17]
print ("Original list:")
print(nums1)
print("\nIs the said list contains all unique elements!")
print(all_unique(nums1)) 

nums2 = [2,4,6,8,10,12,14]
print ("\nOriginal list:")
print(nums2)
print("\nIs the said list contains all unique elements!")
print(all_unique(nums2))

Sample Output:

Original list:
[1, 2, 4, 6, 8, 2, 1, 4, 10, 12, 14, 12, 16, 17]

Is the said list contains all unique elements!
False

Original list:
[2, 4, 6, 8, 10, 12, 14]

Is the said list contains all unique elements!
True

Pictorial Presentation:

Python List: Check if the elements of a given list are unique or not.
Python List: Check if the elements of a given list are unique or not.

Flowchart:

Flowchart: Check if the elements of a given list are unique or not.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

  • Use set() on the given list to keep only unique occurrences.
  • Use len() to compare the length of the unique values to the original list.

Python Code:

def all_unique(test_list):
    return len(test_list) == len(set(test_list))

nums1 = [1,2,4,6,8,2,1,4,10,12,14,12,16,17]
print ("Original list:")
print(nums1)
print("\nIs the said list contains all unique elements!")
print(all_unique(nums1)) 

nums2 = [2,4,6,8,10,12,14]
print ("\nOriginal list:")
print(nums2)
print("\nIs the said list contains all unique elements!")
print(all_unique(nums2))

Sample Output:

Original list:
[1, 2, 4, 6, 8, 2, 1, 4, 10, 12, 14, 12, 16, 17]

Is the said list contains all unique elements!
False

Original list:
[2, 4, 6, 8, 10, 12, 14]

Is the said list contains all unique elements!
True

Flowchart:

Flowchart: Check if the elements of a given list are unique or not.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to extract the nth element from a given list of tuples.
Next: Write a Python program to sort a list of lists by a given index of the inner list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Decapitalizes the first letter of a string:

Example:

def tips_decapitalize(s, upper_rest=False):
  return s[:1].lower() + (s[1:].upper() if upper_rest else s[1:])
print(tips_decapitalize('PythonTips'))
print(tips_decapitalize('PythonTips', True)) 

Output:

pythonTips
pYTHONTIPS

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook