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
- Create a list.
- Use the set() function to find the all unique numbers in the list.
- Convert the set back into a list using the list() function.
- 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
- Create a list and an empty list.
- Use for loop to check whether an element from list1 exists in list2.
- Use the if statement for this purpose.
- If not add that element to the list2.
- 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
Python program to replace the last and the first number of the list with the word.
Python program to check whether the given element is exist in the list or not.
Python program to remove all odd index elements.
Python program to take two lists and return true if then at least one common member.
Python program to convert multiple numbers from a list into a single number.
Python program to convert words of a list into a single string.