Change language

Remove all duplicates from a given string in Python

| |

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 () -" Set is an unordered collection

# data type that is iterative, mutable

# and has no duplicate elements.

# & quot; & quot; .join () -" 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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically