标签云

微信群

扫码加入我们

WeChat QR Code

How do I concatenate two lists in Python?Example:listone = [1, 2, 3]listtwo = [4, 5, 6]Expected outcome:>>> joinedlist[1, 2, 3, 4, 5, 6]


what about if listone was [3,2,1]?Will the output change?

2019年05月24日43分04秒

'merge' as 'create one shallow-copy', 'deep-copy' or 'iterate from'? (Clergyman, it's not at all that trivial)

1970年01月01日00分09秒

Isn’t this operation called concatination rather than merging? I always thought merging means concatination + sorting.

2019年05月23日43分04秒

Do you want to simply append, or do you want to merge the two lists in sorted order? What output do you expect for [1,3,6] and [2,4,5]? Can we assume both sublists are already sorted (as in your example)?

2019年05月24日43分04秒

LenarHoyt Correct. Merging implies some sort of either arbitrary or relational ordering at the very minimum.

2019年05月24日43分04秒

does this create a deep copy of listone and appends listtwo?

2019年05月24日43分04秒

Daniel it will create a new list with a shallow copy of the items in the first list, followed by a shallow copy of the items in the second list. Use copy.deepcopy to get deep copies of lists.

2019年05月23日43分04秒

another useful detail here: listone += listtwo results in listone == [1, 2, 3, 4, 5, 6]

2019年05月24日43分04秒

br1ckb0t will that change what listone is pointing at?So:list3 = listone listone+=listtwo Is list3 changed as well?

2019年05月24日43分04秒

it does change list3. However, if that isn't a problem, it's simpler more readable to add the two lists instead of creating a new one.

2019年05月24日43分04秒

This is better way because it also works with numpy array.

2019年05月24日43分04秒

will this work the same way: mergedList = itertools.chain(listone, listtwo)for item in mergedList:

2019年05月23日43分04秒

d.putto: individual item access is very slow for numpy arrays (each access requires to convert the raw memory with a C type to Python object. Vectorized operations such as np.dot() work on the C types directly without the round trip to Python and therefore fast). You could use merged = np.r_[a, b] to get concatenated numpy array.

2019年05月24日43分04秒

this will lose ordering information.

2019年05月24日43分04秒

True, however, it will also remove duplicates, if that's what you are interested in. List addition along would not do that.

2019年05月24日43分04秒

Better than listone + [x for x in listtwo if x not in listone]

2019年05月24日43分04秒

If I had a list of lists, such as this one:[[0, 5], [1, 10], [0, 7], [3, 5]]How would you merge them to avoid duplicates in the key (first value in each sub-list), but if they are duplicates, end up with the sum of the second values? Like so:[[0, 12], [1, 10], [3, 5]]Thanks

2019年05月23日43分04秒

+1 IMHO this is the correct way to "merge" (union) lists while the "approved" answer describes how to combine/add lists (multiset)

2019年05月23日43分04秒

A nice example of the unpacking approach working on iterable types is functions that return an iterator over one of the lists you're concatenating. For example, you could reverse one of the lists you're concatenating: res = [*l1, *reversed(l2)]. Since reversed returns an iterator, res = l1 + reversed(l2) would throw an error.

2019年05月23日43分04秒

You can use just listone.extend(listtwo) but it wil be deprecated soon

2019年05月23日43分04秒

SergeyIvanov no it won't. list.extend isn't going anywhere.

2019年05月24日43分04秒

And this also works when listone and listtwo have a different number of elements ;-)

2019年05月23日43分04秒

sum(a,[]) only works when "a" is a list of lists.

2019年05月23日43分04秒

Python 3 note: reduce is now in functools so you'll need to import it first.

2019年05月23日43分04秒

Procyclinsur And the list of lists contain integers

2019年05月24日43分04秒

More pythonic way.

2019年05月24日43分04秒

What I looked for

2019年05月24日43分04秒

Note, this assumes that a and b are already sorted

2019年05月24日43分04秒

grabbing dunders is generally not the best approach. If + is off the table, use operator.add.

2019年05月24日43分04秒

The looks a lot prettier, but I guess you would need import operator for that to work.

2019年05月23日43分04秒

Why would the plus operator be unavailable?

2019年05月23日43分04秒

Normally it wouldn't :) but if you are doing list concatenation with the map function or want to store the add function in a variable, you can't use +.

2019年05月23日43分04秒

just so you know, if this is what you're doing in practice, this is much, much slower than the other proposed methods. see stackoverflow.com/questions/17479361/…

2019年05月24日43分04秒

Well, please do some explanation

2019年05月23日43分04秒

And Bushisms, are_funny are bad names!!!

2019年05月23日43分04秒

Already an answer stackoverflow.com/a/48894188/2308683 and stackoverflow.com/a/14453876/2308683

2019年05月24日43分04秒

Its extend and not extends

2019年05月23日43分04秒

Since you are returning, this should be in a function. (Since it's forbidden outside a function)

2019年05月24日43分04秒

this is not true

2019年05月24日43分04秒

You meant print(a) gives the list

2019年05月23日43分04秒

What if i need to add multiple lists, like a+b+c+d+e?

2019年05月24日43分04秒