i have list of lists, i need via Python iterate through each string ,remove spaces (strip) and save list into new list.
E.g. original list: org = [ [' a ','b '],['c ',' d '],['e ',' f'] ]
Expecting new list: new = [ ['a','b'],['c','d'],['e','f'] ]
I started with below code, but no idea how to add stripped objects into new list of lists. new.append(item) - create simple list without inner list.
new = []
for items in org:
for item in items:
item= item.strip()
new.append(item)
You can use a nested list comprehension that strips each word in each sub-list:
new = [[s.strip() for s in l] for l in org]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With