How to remove duplicates from list in Python with code?
Python is the fastest growing language in the world and it is not only hype but it also has some of the best features that any other programming language has.
The built-in Python libraries provide some of the most useful and resources on a variety of platforms.
If you ever get stuck somewhere no problem, the great python community is there to push you through all that said, see how to remove duplicates from a list in python.
Lists are one of four built-in collection data types in python, the other three are tuple, set, and dictionary.
List works like an array, it stores data in an ordered way i.e. first element is at 0order position of second element is 1st location, etc
You can read each and everything about python here.
With just a few lines of code you can easily remove duplicates from a list in python.
You can copy the code here.
L1 = [‘Java’, ‘Python’, ‘Javascript’, ‘C++’,
‘Java’, ‘C’, ‘Swift’, ‘C#’, ‘Kotlin’]
L2 = []
for me in L1:
if I’m not at L2:
L2.append (i)
print (L2)
The code is as simple as printing hello world in any other programming language.
- We took a list L1 with some values
- On line 3, we created an empty list L2, where we will store our results.
- In line 4, we applied for a loop. We iterated each element of the list L1 and assigned each element to the variable ‘i’.
- Next we have an if condition where it is trying to say if an element in ‘i’ is not in L2 then put the content of ‘i’ in L2.
- This appends method simply adds a new element to the end of the list
- Note that when L2 gives the element ‘Java’ a second time for ‘i’ and ‘i’ checks if the element is in L2, it sees that the element is already inside L2 so it doesn’t execute or go inside if its condition simply loops through to the next item in the for loop.
- After successfully completing the for loop, we have finished printing the L2 list.