36. Problem to get all the unique numbers in the list.

In this program, we will take a user input as a list and find all the unique numbers in the list with the help of the below-given steps. There are two methods to solve this problem.

Table of Contents

Unique numbers in a list:

Method 1 :  Using in-built set function

Steps to solve the program
  1. Create a list.
  2. Use the set() function to find the all unique numbers in the list.
  3. Convert the set back into a list using the list() function.
  4. Print the list to see the output.
				
					#Input list
list1 = [12,34,78,12,45,34]

#Printing output
print(list(set(list1)))
				
			

Output :

				
					[34, 12, 45, 78]
				
			

Method 2 : Using for loop

Steps to solve the program
  1. Create a list and an empty list.
  2. Use for loop to check whether an element from list1 exists in list2.
  3. Use the if statement for this purpose. 
  4. If not add that element to the list2.
  5. Print list2.
				
					#Input list
list1 = [12,34,78,12,45,34]

#Creating empty list
list2 = []

for value in list1:
    if value not in list2:
        list2.append(value)

#Printing output
print(list2)
				
			

Output :

				
					[12, 34, 78, 45]

				
			

Related Articles

Get keys and values from the list of dictionaries

Convert a string into a list

Leave a Comment