Python: Remove Duplicates From a List Using Python-DecodingDevOps

Python: Remove Duplicates From a List Using Python-DecodingDevOps

The list is a sequence of items separated by commas and are enclosed in the square brackets. List items contain multiple values which may be repeated multiple numbers of times. In this post i am going to explain how to remove duplicated from the list using python. In order to print the unique list by removing the duplicates using python lets follow the particular steps:

Python Code To Remove Duplicates From a List

Basic_list=[1,2,3,4,4,3,5,3,2]

temp_list = []

for x in basic_list:

      if x not in temp_list:
             
             temp_list.append(x)

Basic_list = temp_list

print(temp_list)

 

STEP 1:  Create a basic list with the elements separated by commas enclosed in square brackets.

STEP 2:  In this step lets create an empty or a temporary list.

STEP 3: Using the python for loop lets check each element in the initialized list that is the basic list.

STEP 4: let’s append elements to the temporary list if those elements are not present.

STEP 5: Now let’s equate the basic list with the temporary list.

Remove The Duplicates From The List Using the Python Set Function

we can also remove the duplicates from the list using the set function which is used to convert any of the repeated elements to the distinct elements.

Basic_list = [1,2,3,4,4,3,5]

List = list(set(Basic_list))

print(List)

Output:

[1,2,3,4,5]

 

Leave a Reply

Your email address will not be published. Required fields are marked *