Chandan Rajpurohit

An Artist With Technical Skills

String or text manipulation is major requirement of modern times. Regex is a option for string manipulation with multiple features.

However, getting hang of regex is complex and people tend to forget syntax.

Today, we will explore a feature with minimum syntax and work to achieve re.sub() capabilities.

Consider, Two list which are mapped one to one with each other. There is a string in which you want to replace all occurrences of items in list one with items in list two.

listA = ['A','B','C']
listB = ['x','y','z']

#original string
ori= "A is programmer in language B and C. C is favourite language of A. B is more rewarding"

#we have to replace all occurrences of A,B,C with x,y,z respectively.

#convert list into dict
dictAB = dict(zip(listA,listB))

#replace all occurrences of key with value in dict
for k,v in dicAB.iteritems:

print(rep)  #display replaced string

More tips and tricks in upcoming post

happy learning 🙂

%d bloggers like this: