1

Let's have, for instance:

nodes = [[1, 2],[3, 4]] 
thelist = [[5, 6], [7, 8]]

How do I code it so list will be:

[[1, 2],[3, 4],[5, 6],[7, 8]]

I know how to do it, but I want an elegant python way.

My try:

for node in nodes:
    thelist.insert(0, node)

I guess there should be a more pythonic way to do that.

EDIT: The order somehow matters (that's why I try to insert at index 0).

Marcelo Zárate
  • 296
  • 5
  • 21
  • Any Python book or tutorial on the internet will teach you how to concatenate lists. By the way, do not use `insert` with a `list` at index `0`. It causes the entire `list` to be reallocated. Lists are optimized for appending not prepending. – OdraEncoded Sep 06 '13 at 17:53
  • Estimated OdraEncoded, i know how to concatenate, and most books teach examples with lists like the post sza mentioned. But in this case, I'll be facing list of lists and list with lists of lists filled with lists haha. And I couldn't find such great answers as the ones you have been posting. edit: Oh, and I didn't insert at the index 0 because of fun the order kinda mattered, I'm gonna to add it to the OP. – Marcelo Zárate Sep 06 '13 at 17:55
  • @sza Not a duplicate, imo, because the proper answer to the linked question is `extend`, which doesn't apply here. Also, why is this being downvoted? There might be a better way to do this in this case, but that doesn't invalidate the question. As phrased in the title, this question is perfectly valid. – flornquake Sep 06 '13 at 18:00
  • Do you need to do anything more than iterate over the combined result? If not, `itertools.chain` might be what you want. If you do frequent inserts and don't need to retrieve items by index you might instead want `collections.deque`. – Steven Rumbalski Sep 06 '13 at 18:01

3 Answers3

11

Simply add them together:

In [11]: nodes + thelist
Out[11]: [[1, 2], [3, 4], [5, 6], [7, 8]]

You can also use extend (which modifies nodes):

In [12]: nodes.extend(thelist)

In [13]: nodes
Out[13]: [[1, 2], [3, 4], [5, 6], [7, 8]]
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
8

You can assign to the slice thelist[:0] to insert elements at the beginning:

nodes = [[1, 2],[3, 4]]
thelist = [[5, 6], [7, 8]]
thelist[:0] = nodes
# thelist is now [[1, 2], [3, 4], [5, 6], [7, 8]]

See the Python tutorial for lots of useful ways to manipulate lists.

flornquake
  • 3,156
  • 1
  • 21
  • 32
  • +1. This assigns to a slice which is exactly what the OP asks for. Although the OP asked for `thetist` to be mutated the more common pattern is to create a new list and reassign to the same name: `theList = nodes + thelist`. – Steven Rumbalski Sep 06 '13 at 17:51
  • +1, that is some interesting syntax that I didn't know about :P – Shashank Sep 06 '13 at 17:53
  • This solution actually could be find in some books, is pretty ellegant and I guess I missed it because I came from other languages where slicing is not possible. Thanks a lot. – Marcelo Zárate Sep 06 '13 at 18:09
5

Alternatively, if some sort of order is important, or just the ability to take items one at a time, then you can use heapq.merge:

import heapq

nodes = [[1, 2],[3, 4]]
thelist = [[5, 6], [7, 8]] 
res = list(heapq.merge(nodes, thelist))
# [[1, 2], [3, 4], [5, 6], [7, 8]]

nodes = [[1, 2], [5,6]]
thelist = [[3, 4], [7, 8]]    
res = list(heapq.merge(nodes, thelist))
# [[1, 2], [3, 4], [5, 6], [7, 8]]

Or, just use:

for heapq.merge(nodes, thelist):

Note that order is potentially different than itertools.chain.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • I'm curious why `heapq.merge` over `itertools.chain`. Is there an advantage to using one over the other? – Steven Rumbalski Sep 06 '13 at 17:54
  • @StevenRumbalski See the second example, list is in sorted order. – Ashwini Chaudhary Sep 06 '13 at 17:57
  • Useful indeed! But I suppose it's not as pythonic as I asked. – Marcelo Zárate Sep 06 '13 at 18:10
  • @MarceloZárate it's perfectly Pythonic if you wanted to merge sequences in order... If you just wish to concat sequences - use any of the other answers you've got :) – Jon Clements Sep 06 '13 at 18:12
  • @MarceloZárate: Over 30k of Jon Clements reputation comes from answering Python questions. By default, you should probably first ask yourself why he thought his answer was Pythonic. (Note -- I'm not claiming that high reputation answerers are infallible, but they are usually a pretty good source.) – Steven Rumbalski Sep 06 '13 at 19:15