0

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']
2
  • 1
    l.extend(s.split()) Commented Dec 8, 2019 at 15:38
  • l = l + s.split() Commented Dec 8, 2019 at 15:38

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0
>>> l += s.split()                                                                                                                             
>>> l                                                                                                                                          
['banana', 'toy', 'ball', 'apple', 'snake', 'car']
>>>

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.