How to fill a new list while iterating over another list?
This should fix it - move the print statement out of the loop, and make a
a string rather than a list.
#Variables
var1 = ['Warehouse Pencil 1.docx', 'Production Pen 20.docx']
list1 = []
for x in var1:
splitted = x.split()
a = splitted[0] + ' ' + splitted[1]
list1.append(a)
print(list1)
Output:
['Warehouse Pencil', 'Production Pen']
You could also use a list comprehension:
>>> [' '.join(x.split()[:2]) for x in var1]
['Warehouse Pencil', 'Production Pen']