views:

642

answers:

5

I have a list like:

list = [[1,2,3],[4,5,6],[7,8,9]]

I want to append a number at the start of every value in the list programmatically, say the number is 9. I want the new list to be like:

list = [[9,1,2,3],[9,4,5,6],[9,7,8,9]]

How do I go about doing this in Python? I know it is a very trivial question but I couldn't find a way to get this done.

+2  A: 

Use the insert method, which modifies the list in place:

>>> numberlists = [[1,2,3],[4,5,6]]
>>> for numberlist in numberlists:
...  numberlist.insert(0,9)
...
>>> numberlists
[[9, 1, 2, 3], [9, 4, 5, 6]]

or, more succintly

[numberlist.insert(0,9) for numberlist in numberlists]

or, differently, using list concatenation, which creates a new list

newnumberlists = [[9] + numberlist for numberlist in numberlists]
Vinko Vrsalovic
That list comprehension you're suggesting as "succint" builds and ignores a list of `None`s - yech!-)
Alex Martelli
I'm aware of that fact. I just didn't mention it :-)
Vinko Vrsalovic
+8  A: 
>>> someList = [[1,2,3],[4,5,6],[7,8,9]]
>>> someList = [[9] + i for i in someList]
>>> someList
[[9, 1, 2, 3], [9, 4, 5, 6], [9, 7, 8, 9]]

(someList because list is already used by python)

cobbal
Woot! TMTOWTDI in Python!! :)
Vinko Vrsalovic
We don't get out of our way to forbid alternatives!-) OTOH, my insert-based solution is better performing and general, so it WILL be "the" obvious way to do it for programmers advanced enough to microbenchmark things (e.g. with python -mtimeit) OR to be familiar with the internals;-).
Alex Martelli
Well, I expect insert() to be faster unless you actually want to create a copy
Vinko Vrsalovic
Nicer since functional ;-)
Dario
+10  A: 
for sublist in thelist:
  sublist.insert(0, 9)

don't use built-in names such as list for your own stuff, that's just a stupid accident in the making -- call YOUR stuff mylist or thelist or the like, not list.

Edit: as the OP aks how to insert > 1 item at the start of each sublist, let me point out that the most efficient way is by assignment of the multiple items to a slice of each sublist (most list mutators can be seen as readable alternatives to slice assignments;-), i.e.:

for sublist in thelist:
  sublist[0:0] = 8, 9

sublist[0:0] is the empty slice at the start of sublist, and by assigning items to it you're inserting the items at that very spot.

Alex Martelli
Thanks for the reply... Just an addendum to the question. What if I want to insert two numbers instead of one. like I want to insert 9,8 at the start of every item in the list?
Aamir
@Aamir: call insert() multiple times, or less efficiently, concatenate the lists: `[9,8] + sublist`.
Nikhil Chelliah
@Amir: or, most efficiently, assign to the empty slice at the start -- let me edit the answer to show that.
Alex Martelli
A: 
#!/usr/bin/env python

def addNine(val):
    val.insert(0,9)
    return val

if __name__ == '__main__':
    s = [[1,2,3],[4,5,6],[7,8,9]]
    print map(addNine,s)

Output:

[[9, 1, 2, 3], [9, 4, 5, 6], [9, 7, 8, 9]]
stefanB
+2  A: 

If you're going to be doing a lot of prepending,
perhaps consider using deques* instead of lists:

>>> mylist = [[1,2,3],[4,5,6],[7,8,9]]

>>> from collections import deque
>>> mydeque = deque()
>>> for li in mylist:
...   mydeque.append(deque(li))
...
>>> mydeque
deque([deque([1, 2, 3]), deque([4, 5, 6]), deque([7, 8, 9])])
>>> for di in mydeque:
...   di.appendleft(9)
...
>>> mydeque
deque([deque([9, 1, 2, 3]), deque([9, 4, 5, 6]), deque([9, 7, 8, 9])])

*Deques are a generalization of stacks and queues (the name is pronounced "deck" and is short for "double-ended queue"). Deques support thread-safe, memory-efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

And, as others have mercifully mentioned:
For the love of all things dull and ugly,
please do not name variables after your favorite data-structures.

Adam Bernier