Python Easy Method to Remove Duplicates in LIST

Here's you can remove duplicates in the List using the SET method. It's a simple method. Just you need SET and Print to remove duplicates. The removing duplicates concept useful when you work in Data science projects.


How to Remove Duplicates in List

A list is a collection of elements. The elements can be duplicates or non-duplicates. Our today's task is to remove duplicate elements that present in the List.


List Python Duplicates

3 Steps to Remove Duplicates
  • Create a List
  • Use SET
  • Print the result


1. Create a List With Duplicate Objects in it

my_list = ['The', 'unanimous', 'Declaration', 'of', 'the', 'thirteen','united', 'States', 'of', 'America,', 'When', 'in', 'the', 'Course', 'of', 'human']

2. Use Set Method Over the List: Below is the Sample Code


>>> non_dupes = set(my_list)

3. Use Print Method to display the Final List

>>> print(non_dupes)

Here, if you observe, there are no duplicates. The duplicates are now removed. It displays only non-duplicate values. 


get rid of duplicates list python


Here 'the' is a duplicate value. That's now removed.

Top List Methods

  1. Append
  2. Extend
  3. Insert
  4. Remove
  5. pop
  6. clear
  7. Index
  8. Count
  9. Sort
  10. Reverse
  11. Copy

Details of List Methods


Append

Adds an item at end of the list.

Extend

Extends the list by adding all the items.

Insert

Inserts an item at a specified location.

Remove

Removes an item from the list. If there are no items, it gives a value error.

pop

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.


Clear

It removes all the items from the list.

Index

It gets an index for the given value. The index starts from '0'.


Count

It returns no of times a particular value appears.

Sort

It sorts the items.


Reverse

Reverses the items in the list.

Copy

Return a shallow copy of the list.

References

Comments

  1. Wow your content is awesome and very helpful and knowledgeable thanks for sharing there kinds of posts apart from that if anyone looking for best institute for Advanced Excel training so contact here +91-9311002620 visithttps://www.htsindia.com/Courses/business-analytics/adv-excel-training-course

    ReplyDelete

Post a Comment

Thanks for your message. We will get back you.

Popular posts from this blog

How to Fix Python Syntax Errors Quickly

7 AWS Interview Questions asked in Infosys, TCS

Hyperledger Fabric: 20 Real Interview Questions