Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepend all list elements into another list [duplicate]

Tags:

python

list

Possible Duplicate:
Merge two lists in python?

How can I prepend list elements into another list?

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

returns

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

How can I make it

['1', '2', '3', '4']?
like image 464
ewhitt Avatar asked May 12 '12 00:05

ewhitt


People also ask

How do I append all elements from one list to another?

append() adds the new elements as another list, by appending the object to the end. To actually concatenate (add) lists together, and combine all items from one list to another, you need to use the . extend() method.

Can there be duplicate elements in a list?

What are duplicates in a list? If an integer or string or any items in a list are repeated more than one time, they are duplicates.

Does list accept duplicate values?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values.


2 Answers

A.extend(B)

or

A += B

This text added to let me post this answer.

like image 65
kindall Avatar answered Oct 21 '22 01:10

kindall


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

like image 35
Cat Plus Plus Avatar answered Oct 21 '22 02:10

Cat Plus Plus