Replace String From Tuple Inside List

arrays, list, python, replace, tuples

Solution

You can use a list comprehension with a conditional expression:

>>> data = [('b','..','o','b'),('t','s','..','t')]
>>> newData = [tuple(s if s != ".." else " " for s in tup) for tup in data]
>>> newData
[('b', ' ', 'o', 'b'), ('t', 's', ' ', 't')]
>>>

Problem

I currently have the following list: ``` data = [('b','..','o','b'),('t','s','..','t')] ``` I am trying to figure out a way to replace all instances of the '..' string to another string. In my instance, the string of ' '. I have attempted to use the built-in function using the following method, but had no luck. ``` newData = list(map(lambda i: str.replace(i, ".."," "), data)) ``` Could someone point me in the right direction? My desired output would be the following: ``` newData = [('b',' ','o','b'),('t','s',' ','t')] ```

Original source

Related problems