Remove Duplicate Strings from a List in Python
Last Updated :
17 Dec, 2024
Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this.
set()
method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.
Python
a = ["Learn", "Python", "With", "GFG", "GFG"]
# Remove duplicates using set
s = list(set(a))
print(s)
Output['with', 'GFG', 'Python', 'Learn']
Explanation:
- The list is converted into a set to eliminate duplicates.
- The set is converted back into a list.
Note: The order of elements changes when using set()
because a set
in Python is an unordered collection of unique elements. Sets do not preserve the order of items as they appear in the original list. Instead, the elements are stored in a way that ensures fast lookups, which can rearrange the order.
Let's explore some more methods and see how we can duplicate strings from a list in Python.
Using dict.fromkeys()
dict.fromkeys()
eliminates duplicates and preserves the order of elements in the list. The keys retain their order as dictionaries in Python maintain insertion order.
Python
# List of strings with duplicates
a = ["Learn", "Python", "with", "GFG", "GFG"]
# Remove duplicates while preserving order
s = list(dict.fromkeys(a))
print(s)
Output['Learn', 'Python', 'with', 'GFG']
Explanation:
dict.fromkeys()
creates a dictionary where keys are unique elements from the list.
Using a for
Loop with a Set
This method iterates through the list while using a set to track already seen elements. By manually checking for duplicates, it ensures that each element is added to the result only once. Since the for loop processes elements sequentially, the original order is preserved.
Python
a = ["Learn", "Python", "with", "GFG", "GFG"]
# Remove duplicates manually
b = []
seen = set()
for word in a:
if word not in seen:
b.append(word)
seen.add(word)
print(b)
Output['Learn', 'Python', 'with', 'GFG']
Explanation:
- A set
seen
is used to track already encountered elements. - Only elements not present in
seen
are added to the 'b'
list.
Using List Comprehension
List comprehension can be combined with a set or a temporary list to remove duplicates. During iteration, it checks if the current element has already been added to a new list, ensuring each element is only added once.
Python
a = ["Learn", "Python", "with", "GFG", "GFG"]
# Remove duplicates using list comprehension
b = []
[b.append(word) for word in a if word not in b]
print(b)
Output['Learn', 'Python', 'with', 'GFG']
Explanation:
- List comprehension iterates through the list and appends elements to a new list only if they are not already present.
Similar Reads
Python - Remove duplicate words from Strings in List Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The
6 min read
Python | Duplicate substring removal from list Sometimes we can come to the problem in which we need to deal with certain strings in a list that are separated by some separator and we need to remove the duplicates in each of these kinds of strings. Simple shorthands to solve this kind of problem is always good to have. Let's discuss certain ways
7 min read
Python | Remove given character from Strings list Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read
Python | Remove Kth character from strings list Sometimes, while working with data, we can have a problem in which we need to remove a particular column, i.e the Kth character from string list. String are immutable, hence removal just means re creating a string without the Kth character. Let's discuss certain ways in which this task can be perfor
7 min read
Python | Remove all digits from a list of strings The problem is about removing all numeric digits from each string in a given list of strings. We are provided with a list where each element is a string and the task is to remove any digits (0-9) from each string, leaving only the non-digit characters. In this article, we'll explore multiple methods
4 min read
Python - Remove leading 0 from Strings List Sometimes, while working with Python, we can have a problem in which we have data which we need to perform processing and then pass the data forward. One way to process is to remove a stray 0 that may get attached to a string while data transfer. Let's discuss certain ways in which this task can be
5 min read