{"id":5008,"date":"2023-10-26T15:29:20","date_gmt":"2023-10-26T09:59:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=5008"},"modified":"2023-11-10T12:01:36","modified_gmt":"2023-11-10T06:31:36","slug":"python-how-to-remove-duplicates-from-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-remove-duplicates-from-a-list\/","title":{"rendered":"Python : How to Remove Duplicates from a List"},"content":{"rendered":"
A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed.<\/p>\n
The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list elements. The same value may appear multiple times in a list. Each event is regarded as a distinct element.<\/p>\n
Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can delete duplicate elements by many methods some of them are:<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n For unique elements, we’ll need to make a new list. Then iterate through the original list, adding each element to the newList only if it isn’t already there.<\/p>\n Below is the implementation:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n A collection is an ordered list of values. There could be various types of values. A list is a mutable container. This means that existing ones can be added to, deleted from, or changed. The Python list represents the mathematical concept of a finite sequence. List values are referred to as list items or list …<\/p>\ngivenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']<\/pre>\n
Given list with duplicates : ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']\r\nGiven list without duplicates : ['hello', 'this', 'is', 'BTechGeeks']<\/pre>\n
Delete Duplicates elements from the list<\/h2>\n
\n
Method #1:Using set<\/h3>\n
\n
# given list (which contains duplicates)\r\ngivenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']\r\nprint('Given list with duplicates : ', givenlist)\r\n# converting givenlist to set and then back to list to remove duplicates\r\ngivenlist = list(set(givenlist))\r\n# print given list\r\nprint('Given list without duplicates : ', givenlist)\r\n<\/pre>\n
Given list with duplicates : ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']\r\nGiven list without duplicates : ['this', 'is', 'hello', 'BTechGeeks']<\/pre>\n
While this method avoids duplicates, it does not hold the elements in the same order as the original.<\/pre>\n
Method #2:Using in operator and lists (same order)<\/h3>\n
# given list (which contains duplicates)\r\ngivenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']\r\nprint('Given list with duplicates : ', givenlist)\r\n# taking empty list\r\nnewlist = []\r\n# Travers the given list\r\nfor element in givenlist:\r\n # if the element is not present in new list then append it\r\n if element not in newlist:\r\n newlist.append(element)\r\n# print new list\r\nprint('Given list without duplicates : ', newlist)\r\n<\/pre>\n
Given list with duplicates : ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']\r\nGiven list without duplicates : ['hello', 'this', 'is', 'BTechGeeks']<\/pre>\n
Method #3:Using Counter() function (same order)<\/h3>\n
\n
from collections import Counter\r\n# given list (which contains duplicates)\r\ngivenlist = ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']\r\nprint('Given list with duplicates : ', givenlist)\r\n# counting the frequencies using Counter function\r\nfreq = Counter(givenlist)\r\n# converting keys to list\r\nuniquelist = list(freq.keys())\r\n# print unqiue list\r\nprint('Given list without duplicates : ', uniquelist)\r\n<\/pre>\n
Given list with duplicates : ['hello', 'this', 'is', 'hello', 'BTechGeeks', 'is', 'this']\r\nGiven list without duplicates : ['hello', 'this', 'is', 'BTechGeeks']<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n