Q:

Unable to remove whitespaces from list items

Would request you to help me out on this , I am unable to remove white spaces from list elements which show unnecessary whitespaces . Can you please help me remove it .
I need to fetch lines having ip 1.1.1.1/32 and 3.3.3.3/32 in the beginning and fetch all the items in a list.
I tried the same through regex, the white spaces are removed but i am unable to fetch the last digit item from table .
Please provide the solution for the same :
import re
from io import StringIO

output = """
---------------------------------------------------------------------------
                  |           Up              |         Down              |
  Net     Prefix  |---------------------------+---------------------------
                  |       ASR        |  L     |       A          | Label  |
------------------+------------------+--------+------------------+---------
        1.1.1.1/32|  162.148.102.2:0 |     102|         -        |    -   |
     10.10.40.0/24|  162.148.102.2:0 |     101|         -        |    -   |
        1.1.1.1/32|  162.148.102.2:0 |     100|         -        |    -   |
        3.3.3.3/32|         -        |    -   |  162.148.102.2:0 |     103|
  192.158.104.0/24|         -        |    -   |  162.148.102.2:0 |     105|
        2.2.2.2/32|         -        |    -   |  162.148.102.2:0 |     106|
     10.30.50.0/24|         -        |    -   |  162.148.102.2:0 |     107|

---------------------------------------------------------------------------
"""

a = "1.1.1.1/32"
b = "3.3.3.3/32"
output  = StringIO (output)

i = 0
j = 0
for line in output.read().split('\n'):
    if b in line:
        tempa_i = line.strip().split('|')
        res_i = [ele for ele in tempa_i if ele.strip(' ')]
        i = i+1
    if a in line:
        tempb_j = re.findall(r'(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})',line)
        j = j+1

print (i)
print (j)

for list_ in range (i):
    print((res_i))
for list_ in range (j):
    print(tempb_j)

Observed Output :
['3.3.3.3/32', '         -        ', '    -   ', '  162.148.102.2:0 ', '     103']
['1.1.1.1', '162.148.102.2']
['1.1.1.1', '162.148.102.2']

Expected output:
['3.3.3.3/32', '-', '-', '162.148.102.2:0', '103']
['1.1.1.1/32', '162.148.102.2:0', '102', '-', '-']
['1.1.1.1/32', '162.148.102.2:0', '100', '-', '-']

A:

You can use a list comprehension, and the str.strip() method to remove all the white-spaces on the sides of all the strings in the list:
lst = [...]
lst = [s.strip() for s in lst]

If you want to remove all the white-spaces in the strings, not just on the sides, you can use str.replace():
lst = [...]
lst = [s.replace(' ','') for s in lst]

