Remove all duplicates from a given string in Python

File handling | Python Methods and Functions | String Variables

We are given a string, and we need to remove all duplicates from it? What will be the output if the order of the characters matters? 
Examples:

 Input: pythonengineering Output: efgkos 

This problem has an existing solution, please refer to Remove all duplicates from the given string .

from collections import OrderedDict

 
# Function to remove all duplicates from a string
# and no order values ​​

def removeDupWithoutOrder ( str ):

 

# set () - & gt; Set is an unordered collection

# data type that is iterative, mutable

# and has no duplicate elements.

# & quot; & quot; .join () - & gt; Concatenates two adjacent elements in

# repeats with any character defined in

# & quot; & quot; (double quotes) and returns

# single line

return "". join ( set ( str ))

 
# Function to remove all duplicates from a string
# and follow the order of the characters

def removeDupWithOrder ( str ):

return " ". join (OrderedDict.fromkeys ( str )) 

 
# Driver program

if __ name__ = = "__ main__" :

str = "pythonengineering"

print " Without Order = " , removeDupWithoutOrder ( str )

print "With Order =" , removeDupWithOrder ( str )

Output:

 Without Order = egfkosr With Order = geksfor 

What Or Do deredDict and fromkeys ()?

OrderedDict — it is a dictionary that remembers the order of the keys that were inserted first. If a new record overwrites an existing record, the original insertion position remains unchanged.

For example, see below code snippet:

from collections import OrderedDict

 

ordinary_dictionary = {}

ordinary_dictionary [ 'a' ] = 1

ordinary_dictionary [ 'b' ] = 2

ordinary_dictionary [ ' c' ] = 3

ordinary_dictionary [ ' d' ] = 4

ordinary_dictionary [ 'e' ] = 5

 
# Exit = {& # 39; a & # 39 ;: 1, & # 39; c & # 39 ;: 3, & # 39; b & # 39 ;: 2, & # 39; e & # 39 ;: 5, & # 39; d & # 39 ;: 4}

print ordinary_dictionary 

 

ordered_dictionary = OrderedDict ()

ordered_dictionary [ 'a' ] = 1

ordered_dictionary [ 'b' ] = 2

ordered_dictionary [ 'c' ] = 3

ordered_dictionary [ 'd' ] = 4

ordered_dictionary [ 'e' ] = 5

 
# Output = {& # 39; a & # 39 ;: 1, & # 39; b & # 39 ;: 2, & # 39; c & # 39 ;: 3, & # 39; d & # 39 ;: 4, & # 39; e & # 39 ;: 5}

print ordered_dictionary 

fromkeys () creates a new dictionary with keys from seq and values ​​set to value, and returns a list of keys, fromkeys (seq [, value]) — this is the syntax for the fromkeys () method.

Parameters:

  • seq: this is a list of values ​​that will be used to prepare dictionary keys.
  • value: this is optional, if specified, the value will be set to this value.

For example, see below code snippet:

from collections import OrderedDict

seq = ( 'name' , 'age' , ' gender' )

dict = OrderedDict.fromkeys (seq)

 
# Output = {& # 39; age & # 39 ;: None, & # 39; name & # 39 ;: None, & # 39; gender & # 39 ;: None}

print str ( dict

dict = OrderedDict.fromkeys (seq, 10 )

  
# Output = {& # 39; age & # 39 ;: 10, & # 39; name & # 39 ;: 10, & # 39; gender & # 39 ;: 10}

print str ( dict

This article provided by Shashank Mishra oh (Gull) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.





Tutorials