9 examples of 'remove duplicates from list python' in Python

Every line of 'remove duplicates from list python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
162def remove_duplicates(l):
163 """Remove duplicates from a list (but keep the order)."""
164 seen = set()
165 results = []
166 for e in l:
167 if e not in seen:
168 results.append(e)
169 seen.add(e)
170 return results
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

76def remove_duplicates (self):
77 # Assumes list has been sorted!
78 for i in range(len(self.files) - 1, 0, -1):
79 if self.files[i] == self.files[i - 1]:
80 del self.files[i]
58def remove_duplicates(l):
59 return [list(x) for x in set(tuple(x) for x in l)]
155def remove_duplicates(self, list_):
156 """Returns a list without duplicates based on the given ``list``.
157
158 Creates and returns a new list that contains all items in the given
159 list so that one item can appear only once. Order of the items in
160 the new list is the same as in the original except for missing
161 duplicates. Number of the removed duplicates is logged.
162 """
163 self._validate_list(list_)
164 ret = []
165 for item in list_:
166 if item not in ret:
167 ret.append(item)
168 removed = len(list_) - len(ret)
169 logger.info('%d duplicate%s removed.' % (removed, plural_or_not(removed)))
170 return ret
392def _check_remove_item(the_list, item):
393 """Helper function for merge_lists that implements checking wether an items
394 should be removed from the list and doing so if needed. Returns ``True`` if
395 the item has been removed and ``False`` otherwise."""
396 if not isinstance(item, basestring):
397 return False
398 if not item.startswith('~'):
399 return False
400 actual_item = item[1:]
401 if actual_item in the_list:
402 del the_list[the_list.index(actual_item)]
403 return True
45def findRemove(listR, value):
46 """used to test if a value exist, if it is, return true and remove it."""
47 try:
48 listR.remove(value)
49 return True
50 except ValueError:
51 return False
86def remove_duplicated_files_from_list(file_list):
87 """
88 Split filenames from their paths, and remove duplicates, keeping the
89 first ocurrence in the list.
90
91 Example
92
93 input :
94 ["C:\dir1\file1.nxs","C:\dir1\file2.nxs","C:\dir1\dir2\file1.nxs"]
95
96 output :
97 ["C:\dir1\file1.nxs","C:\dir1\file2.nxs"]
98 """
99 files = [os.path.basename(full_path) for full_path in file_list]
100 unique_files = [file_list[n] for n, file_name in enumerate(files) if file_name not in files[:n]]
101 return unique_files
154def test_remove_on_list(test_lists):
155 """Test remove work for node in list."""
156 test_lists[2].remove(4)
157 assert test_lists[2].size() is 4
484def deduplicate(lst):
485 i = 0
486 while i < len(lst):
487 if lst[i] in lst[0 : i]:
488 lst.pop(i)
489 i -= 1
490 i += 1
491 return lst

Related snippets