3

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.

5 Answers 5

16
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.

Sign up to request clarification or add additional context in comments.

4 Comments

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: call insert() multiple times, or less efficiently, concatenate the lists: [9,8] + sublist.
@Amir: or, most efficiently, assign to the empty slice at the start -- let me edit the answer to show that.
After benchmarking with timeit, the [0:0] method is almost 2x faster than the insert() one. Mostly because it avoids a function call I guess. Anyway, it's less readable.
12
>>> 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)

3 Comments

Woot! TMTOWTDI in Python!! :)
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;-).
Well, I expect insert() to be faster unless you actually want to create a copy
2

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]

2 Comments

That list comprehension you're suggesting as "succint" builds and ignores a list of Nones - yech!-)
I'm aware of that fact. I just didn't mention it :-)
2

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.

Comments

0
#!/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]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.