This is the final chapter of the lists in python topic, in this chapter we will create an example that will remove the duplicate student names within a student list with the help of the python loop.
We are given a list of student names, our mission is to remove the duplicate student name from the list.
student = ["Rick", "Ricky", "Richard", "Rick", "Rickky", "Rick", "Rickky"] # there are names that have been duplicated double_enter = [] # first we need to know all the duplicate student names for i in range(len(student)): count = student.count(student[i]) if count > 1: if student[i] not in double_enter: double_enter.append(student[i]) # then we will remove those duplicate student names for name in student: if name in double_enter and student.count(name) > 1: student.remove(name) print(student)

The above python program is very long, can you find a shorter method to achieve the above outcome? Write your own solution in the comment box below.
Please follow and like us:
student = list(set(student)) # if you need a list, otherwise just use set()
print(student)
There are several ways to do it, this Stack Overflow question is a good summary:
https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists
Briefly, if order doesn’t matter, list(set(your_list)) does the job; if order matters you can use an OrderedDict to do something similar; if the contents of the list are not hashable, the fourth answer in that link has a loop that is simpler than yours.
students = [“Rick”, “Ricky”, “Richard”, “Rick”, “Rickky”, “Rick”, “Rickky”]
print(list(set(students)))