0

Looks like im missing something very simple here. I have list of elements in this format now few elements in the list have '\t' (tab or multiple tabs). Now instead of '\t' i want to store them as tab in the list. I iterating over each element and doing .replace('\t', ' ') clearly this isnt working.

l = ['test', '\tabc', '\t\tcde']

I want this to be stored as

l = ['test', 'abc', 'cde'] #It isnt allowing to add whitespaces I am looking to add is 2 space or 4 space indent based on number of \t infront of an element.

Can someone please help?

kittu
  • 3
  • 3
  • They **are strored as tabs in the list**. A `\t` in the `repr` of a `str` stands for a tab. – juanpa.arrivillaga Feb 07 '22 at 18:45
  • Does this answer your question? [Removing character in list of strings](https://stackoverflow.com/questions/8282553/removing-character-in-list-of-strings) – Tomerikoo Feb 07 '22 at 19:59

1 Answers1

0

Here is my attempt:

l = ['test', '\tabc', '\t\tcde']
l = list(map(lambda elem: elem.replace('\t', ' '), l))

print(l)

Output:

['test', ' abc', '  cde']
Richard K Yu
  • 2,152
  • 3
  • 8
  • 21