philthy
philthy

Reputation: 33

Python; Search and Replace; Lists; Strings

    a = self.test_lockCheck():
    d = []
    for i in a.iteritems():
        d = a.replace('1','3')

a instantiates a function which return's a list which contains a string for each of its indices. Each string indice will always contain a '1' which needs to be replaced by '3'. I found replace() in another question on this site. I was curious if anyone knew of another way? Thanks!

Upvotes: 0

Views: 541

Answers (1)

arshajii
arshajii

Reputation: 129537

You can simply use a list comprehension, instead of trying to create a new list via a for-loop:

a = [s.replace('1','3') for s in self.test_lockCheck()]

If you're dealing with a dictionary, you can still apply something similar:

a = {k.replace('1','3'): v for k,v in self.test_lockCheck().iteritems()}

Upvotes: 2

Related Questions