How to add all list items to another list

Possible duplicate:
Merge two lists in python?

How can I add list items to another list?

A = ['1', '2'] B = ['3', '4'] A.append(B) print A 

returns

 ['1', '2', ['3', '4']] 

How can I do it

 ['1', '2', '3', '4']? 
+6
source share
3 answers
 A.extend(B) 

or

 A += B 

This text is added so that I can post this answer.

+9
source

list.extend , for example, in your case A.extend(B) .

+1
source

it can also be done as

 for s in B: A.append(B) 

Of course, A.extend(B) does the same job, but using append , we need to add to the above f

-2
source

Source: https://habr.com/ru/post/915544/


All Articles