Home > Back-end >  How to store string with tabs in a list in python
How to store string with tabs in a list in python

Time:02-08

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?

CodePudding user response:

Here is my attempt:

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

print(l)

Output:

['test', ' abc', '  cde']

CodePudding user response:

try this:


for item in l:
    for i in item:
        if i == '\t':
            i.replace('\t', '')

print(l)
  •  Tags:  
  • Related