Creating a list with holes

2014-01-03 Thread Larry Martell
I think I know the answer is no, but is there any package that allows creating a list with holes in it? E.g. I'd want to do something like: x[10] = 12 x[20] = 30 I'm thinking of something like defaultdict but for lists (I know that's very different, but ... ) Thanks! -larry -- https

Re: Creating a list with holes

2014-01-03 Thread eneskristo
On Friday, January 3, 2014 4:19:09 PM UTC+1, larry@gmail.com wrote: I think I know the answer is no, but is there any package that allows creating a list with holes in it? E.g. I'd want to do something like: x[10] = 12 x[20] = 30 I'm thinking of something like defaultdict

Re: Creating a list with holes

2014-01-03 Thread Chris Angelico
On Sat, Jan 4, 2014 at 2:19 AM, Larry Martell larry.mart...@gmail.com wrote: I think I know the answer is no, but is there any package that allows creating a list with holes in it? E.g. I'd want to do something like: x[10] = 12 x[20] = 30 I'm thinking of something like defaultdict

Re: Creating a list with holes

2014-01-03 Thread Roy Smith
In article mailman.4852.1388762356.18130.python-l...@python.org, Larry Martell larry.mart...@gmail.com wrote: I think I know the answer is no, but is there any package that allows creating a list with holes in it? E.g. I'd want to do something like: x[10] = 12 x[20] = 30 Whenever you ask

Re: Creating a list with holes

2014-01-03 Thread Larry Martell
On Fri, Jan 3, 2014 at 10:30 AM, eneskri...@gmail.com wrote: On Friday, January 3, 2014 4:19:09 PM UTC+1, larry@gmail.com wrote: I think I know the answer is no, but is there any package that allows creating a list with holes in it? E.g. I'd want to do something like: x[10] = 12 x[20

Re: Creating a list with holes

2014-01-03 Thread Chris Angelico
On Sat, Jan 4, 2014 at 2:38 AM, Roy Smith r...@panix.com wrote: Why do you want holes? Is the issue that you're storing sparse data and don't want to waste memory on unused keys? If so, a dictionary should do you fine. Do you need to be able to read the values back out in a specific order?

Re: Creating a list with holes

2014-01-03 Thread Larry Martell
On Fri, Jan 3, 2014 at 10:37 AM, Chris Angelico ros...@gmail.com wrote: On Sat, Jan 4, 2014 at 2:19 AM, Larry Martell larry.mart...@gmail.com wrote: I think I know the answer is no, but is there any package that allows creating a list with holes in it? E.g. I'd want to do something like: x[10

Re: Creating a list with holes

2014-01-03 Thread Chris Angelico
On Sat, Jan 4, 2014 at 2:51 AM, Roy Smith r...@panix.com wrote: In article mailman.4853.1388763434.18130.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: Alternatively, if you expect to fill in most of the elements, it's possible you'd be happier working with a subclass of list

Re: Creating a list with holes

2014-01-03 Thread Roy Smith
In article mailman.4853.1388763434.18130.python-l...@python.org, Chris Angelico ros...@gmail.com wrote: Alternatively, if you expect to fill in most of the elements, it's possible you'd be happier working with a subclass of list that auto-expands by filling in the spare space with a singleton

Re: Creating a list with holes

2014-01-03 Thread Chris Angelico
On Sat, Jan 4, 2014 at 2:55 AM, Larry Martell larry.mart...@gmail.com wrote: The use case is that I'm parsing a XML file like this: Parameter Name=DefaultVersion Values Value DefaultTrue/Default /Value /Values

Re: Creating a list with holes

2014-01-03 Thread Denis McMahon
On Fri, 03 Jan 2014 10:41:21 -0500, Larry Martell wrote: The holes would be between the items I put in. In my example above, if I assigned to [10] and [20], then the other items ([0..9] and [11..19]) would have None. dic = { 10:6, 20:11} dic.get(10) 6 dic.get(14) dic.get(27,oh god there's

Re: Creating a list with holes

2014-01-03 Thread Larry Martell
On Fri, Jan 3, 2014 at 1:07 PM, Denis McMahon denismfmcma...@gmail.com wrote: On Fri, 03 Jan 2014 10:41:21 -0500, Larry Martell wrote: The holes would be between the items I put in. In my example above, if I assigned to [10] and [20], then the other items ([0..9] and [11..19]) would have

Re: Creating a list with holes

2014-01-03 Thread Larry Martell
On Fri, Jan 3, 2014 at 11:07 AM, Chris Angelico ros...@gmail.com wrote: On Sat, Jan 4, 2014 at 2:55 AM, Larry Martell larry.mart...@gmail.com wrote: The use case is that I'm parsing a XML file like this: Parameter Name=DefaultVersion Values Value

Re: Creating a list with holes

2014-01-03 Thread Roy Smith
In article mailman.4871.1388794533.18130.python-l...@python.org, Larry Martell larry.mart...@gmail.com wrote: Thanks, but I know all that about dicts. I need to use a list for compatibility with existing code. Generalizing what I think the situation is, A dict is the best data structure

Re: Creating a list with holes

2014-01-03 Thread Chris Angelico
On Sat, Jan 4, 2014 at 11:18 AM, Larry Martell larry.mart...@gmail.com wrote: Your last suggestion is what I ended up doing, but I had to key off the Values /Values unit - I couldn't use Value because that isn't present for ones that have no Current - that messed me up for hours. But it's

Re: Creating a list with holes

2014-01-03 Thread Denis McMahon
On Fri, 03 Jan 2014 20:18:06 -0500, Roy Smith wrote: In article mailman.4871.1388794533.18130.python-l...@python.org, Larry Martell larry.mart...@gmail.com wrote: Thanks, but I know all that about dicts. I need to use a list for compatibility with existing code. Generalizing what I

Re: Creating a list with holes

2014-01-03 Thread Dan Stromberg
On Fri, Jan 3, 2014 at 7:37 AM, Chris Angelico ros...@gmail.com wrote: Depending on what exactly you need, it's probably worth just using a dict. In what ways do you need it to function as a list? You can always iterate over sorted(some_dict.keys()) if you need to run through them in order.

Re: Creating a list with holes

2014-01-03 Thread Chris Angelico
On Sat, Jan 4, 2014 at 1:58 PM, Dan Stromberg drsali...@gmail.com wrote: On Fri, Jan 3, 2014 at 7:37 AM, Chris Angelico ros...@gmail.com wrote: Depending on what exactly you need, it's probably worth just using a dict. In what ways do you need it to function as a list? You can always iterate

Re: Creating a list with holes

2014-01-03 Thread Dan Stromberg
On Fri, Jan 3, 2014 at 7:00 PM, Chris Angelico ros...@gmail.com wrote: On Sat, Jan 4, 2014 at 1:58 PM, Dan Stromberg drsali...@gmail.com wrote: On Fri, Jan 3, 2014 at 7:37 AM, Chris Angelico ros...@gmail.com wrote: Depending on what exactly you need, it's probably worth just using a dict. In

Re: Creating a list with holes

2014-01-03 Thread Chris Angelico
On Sat, Jan 4, 2014 at 2:32 PM, Dan Stromberg drsali...@gmail.com wrote: That is fine, sorting once at then end of a script is a good use of sorted(some_dict.keys()). However, it probably should be pointed out that this, while similar, is not so good: for thing in range(n): for key in

Re: Creating a list with holes

2014-01-03 Thread Frank Millman
Larry Martell larry.mart...@gmail.com wrote in message news:CACwCsY5P47-dB1NLQTUTQ=0aF6B+-M3y4hCxcUGmcVmHM8=-x...@mail.gmail.com... I think I know the answer is no, but is there any package that allows creating a list with holes in it? E.g. I'd want to do something like: x[10] = 12 x[20