4

I'm so new to Python that the only way I can code so far is by blindly waving my keyboard around. So I'm sure there's an excellent reason why the following doesn't work:

l = []

grouping = compactlist.index(namelist[n])
l[grouping].append(start[n])
l[grouping].append(end[n])

So what I'm trying to do is take a value from the start list and add it to a list in l - which list it'll be is dependendent on the value of grouping. (Then do the same for end). This requires the lists within l to be created on the fly, which I assume is the problem.

2 Answers 2

8

You could initiate l like l = [[], []], but it actually sounds more like you want to use a defaultdict as your data structure. This can create your lists on the fly, e.g.

>>> import collections
>>> thing = collections.defaultdict(list)
>>> thing[0].append('spam')
>>> thing[1].append('eggs')
>>> print thing
defaultdict(<type 'list'>, {0: ['spam'], 1: ['eggs']})
>>> thing[0]
['spam']
>>> thing[69]
[]
Sign up to request clarification or add additional context in comments.

3 Comments

This is the way to do it, if you can't tell in advance what keys you will need. Use a dictionary at the top level, rather than a list. And a defaultdict means that you don't need to create the lists explicitly.
The only potential problem with using a dict or defaultdict is that there's no inherent sequential ordering of the keys, which may or may not matter depending on what you're doing and whether (or how) you want to iterate over their associated values.
Thanks, I think I'll try experimenting with defaultdict. Ordering doesn't matter, luckily.
0

Try starting with

l = [[],[]]

That will give you a list of two empty lists, and you can append to either of them using the code that you already have written.

Edit

As you mention in the comment, you will need more than two lists. If you know in advance how many you will need, then you can extend this technique like this:

l = [[] for x in range( number_of_lists )]

If you don't know in advance, though, or if you aren't necessarily generating nice regular numbers like 0, 1, 2, 3, ... , then you don't want a list of lists*. In that case, you want a dictionary of lists. @wim's answer is the best solution in that case.

* Perhaps, instead, your grouping values are going to be 100, 20000, 300000, etc. You really don't want a list in that case.

3 Comments

Ack, my mistake! grouping won't only come up with either 0 or 1, there may be more values than that. Sorry! Editing now.
@lowercasename l = [[]] * number_of_lists is almost certainly not what you want. It creates a list containing number_of_lists references to a single empty list. Usually one never wants this, as it makes the behaviour of l[n].append(something) very unintuitive; try entering the following at a Python prompt: l = [[]] * 10; print l; l[3].append('weird'); print l.
To avoid what @Ben is talking about use l = [[] for i in range(number_of_lists)].

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.