We are given a string and we need to remove all duplicates from it? What will be the output if the order of character matters?
Examples:
Input : geeksforgeeks Output : efgkos
This problem has existing solution please refer Remove all duplicates from a given string.
Method 1:
from collections import OrderedDict # Function to remove all duplicates from string # and order does not matter def removeDupWithoutOrder(str): # set() --> A Set is an unordered collection # data type that is iterable, mutable, # and has no duplicate elements. # "".join() --> It joins two adjacent elements in # iterable with any symbol defined in # "" ( double quotes ) and returns a # single string return "".join(set(str)) # Function to remove all duplicates from string # and keep the order of characters same def removeDupWithOrder(str): return "".join(OrderedDict.fromkeys(str)) # Driver program if __name__ == "__main__": str = "geeksforgeeks" print ("Without Order = ",removeDupWithoutOrder(str)) print ("With Order = ",removeDupWithOrder(str)) |
Output:
Without Order = egfkosr With Order = geksfor
Method 2:
def removeDuplicate(str): s=set(str) s="".join(s) print("Without Order:",s) t="" for i in str: if(i in t): pass else: t=t+i print("With Order:",t) str="geeksforgeeks"removeDuplicate(str) |
Output:
Without Order: rofgeks With Order: geksfor
What do OrderedDict and fromkeys() do ?
An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged.
For example see below code snippet :
from collections import OrderedDict ordinary_dictionary = {} ordinary_dictionary['a'] = 1ordinary_dictionary['b'] = 2ordinary_dictionary['c'] = 3ordinary_dictionary['d'] = 4ordinary_dictionary['e'] = 5 # Output = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} print (ordinary_dictionary) ordered_dictionary = OrderedDict() ordered_dictionary['a'] = 1ordered_dictionary['b'] = 2ordered_dictionary['c'] = 3ordered_dictionary['d'] = 4ordered_dictionary['e'] = 5 # Output = {'a':1,'b':2,'c':3,'d':4,'e':5} print (ordered_dictionary) |
fromkeys() creates a new dictionary with keys from seq and values set to value and returns list of keys, fromkeys(seq[, value]) is the syntax for fromkeys() method.
Parameters :
- seq : This is the list of values which would be used for dictionary keys preparation.
- value : This is optional, if provided then value would be set to this value.
For example see below code snippet :
from collections import OrderedDict seq = ('name', 'age', 'gender') dict = OrderedDict.fromkeys(seq) # Output = {'age': None, 'name': None, 'gender': None} print (str(dict)) dict = OrderedDict.fromkeys(seq, 10) # Output = {'age': 10, 'name': 10, 'gender': 10} print (str(dict)) |
This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Remove all duplicates words from a given sentence
- Remove all consecutive duplicates from the string
- Remove duplicates from a given string
- Python groupby method to remove all consecutive duplicates
- Python | Remove all duplicates and permutations in nested list
- Python - Remove K length Duplicates from String
- Python | Sort given list by frequency and remove duplicates
- Recursively remove all adjacent duplicates
- Remove three consecutive duplicates from string
- Remove duplicates from a string in O(1) extra space
- Remove duplicates from string keeping the order according to last occurrences
- Python Remove Duplicates from a List
- Python - Ways to remove duplicates from list
- Python | Remove consecutive duplicates from list
- Python | Remove duplicates from nested list
- Python | Remove duplicates in Matrix
- Python | Remove duplicates based on Kth element tuple list
- Python - Remove Equilength and Equisum Tuple Duplicates
- Python - Remove Kth Index Duplicates in Tuple
- Python - Remove alternate consecutive duplicates
Improved By : vs164758

