Monday, March 30, 2020

IslandT: Lists in python example

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)

Both Ricky and Rickky have duplicated values

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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...