My Code is splitting up a file line by line into strings and extends them to a list.
I noticed that for cases where only one string is extended, the result will split up that string and extend it letter by letter to the empty list. If I delete [-1] the string remains intact, but the other strings will be extended aswell.
How can I prevent that string from being split up when extending it to the empty list?
searchstring = "abc dfe ghi" #resembles a searchline from a file
text = searchstring.split() #note: same thing happens if i add [-1] here
list1.extend(text[-1]) #I only want the last element of the string
So either the output is:
print list1
[abc, dfe, ghi]
or
print list1
[g, h, i]
but I need it this way
print list1
[ghi] #one entry for each line in the file
list.extend
accepts an iterable.
So when you pass it a string (ghi
) it uses it like a list, extending list with chars from that string.
You may want to put that string to a list or use list.append
.