Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a string and a list like this-
s = "ball apple snake car" l = ['banana','toy']
Now I want to add it to an existing list so that the list looks like-
l = ['banana','toy','ball' ,'apple' ,'snake ','car']
l.extend(s.split())
l = l + s.split(' ') > ['banana', 'toy', 'ball', 'apple', 'snake', 'car']
With s.split(' '), you transform the string to a list of the words. With l +, you append that list to the end of the other list.
s.split(' ')
l +
Add a comment
Well it seems the simplest answer is to split the string by the spaces and append it to the list:
s="ball apple snake car" l=['banana','toy'] s_list = s.split() for item in s_list: l.append(item)
>>> l += s.split() >>> l ['banana', 'toy', 'ball', 'apple', 'snake', 'car'] >>>
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
l.extend(s.split())