Table of Contents

Python code to remove redundant data from a list

 

Python code implementation without user defined functions & classes

Code: 
# A list containing redundant data
a = [1, 3, 4, 1, 2, 2, 5, 2, 1, 7, 0, 9]

#Take an Empty List
c = []

# Traverse the list a using for loop
# Add the element to the list c if it does not exist
for i in a:
    if i not in c:
        c.append(i)

# Printing the result

print(c)

 

Output: 
[1, 3, 4, 2, 5, 7, 0, 9]
Python Code Editor Online - Click to Expand

Python code implementation using function

In this code we will take a list containing redundant data. We will pass the list to a function that will return the list after removing the redundant data.

Code: 
def remove_redundant_data(b):
    c = []
    for i in b:
        if i not in c:
            c.append(i)
    return c


# A list containing redundant data
a = [1, 3, 4, 1, 2, 2, 5, 2, 1, 7, 0, 9]

# Pass the list to the function and storing the result to another list
final_list = remove_redundant_data(a)
print(final_list)

 

Output: 
[1, 3, 4, 2, 5, 7, 0, 9]
Python Code Editor Online - Click to Expand

Python code implementation using Classes

In this code we will take a list containing redundant data. We will implement a class that contain function to remove redundant data from a list. We will create an object and will call the function using object.function(), the function will return the list after removing the redundant data.

Code: 
class Remove_redundant_data(object):
    def __init__(self):
        pass
    def remove_redundant_data(self, a):
        self.a = a
        c = []
        for i in self.a:
            if i not in c:
                c.append(i)
        return c



# A list containing redundant data
a = [1, 3, 4, 1, 2, 2, 5, 2, 1, 7, 0, 9]

#Create an object
final_list = Remove_redundant_data()

#Calling the function using object.function
print(final_list.remove_redundant_data(a))

 

Output: 
[1, 3, 4, 2, 5, 7, 0, 9]
Python Code Editor Online - Click to Expand