Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63079

Re: Creating a list with holes

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: Creating a list with holes
Date 2014-01-03 18:07 +0000
Organization A noiseless patient Spider
Message-ID <la6u8e$lf9$3@dont-email.me> (permalink)
References <mailman.4852.1388762356.18130.python-list@python.org> <c37ad1f1-1367-4de0-9a2e-bc60355abea3@googlegroups.com> <mailman.4856.1388763689.18130.python-list@python.org>

Show all headers | View raw


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 nothing here")
"oh god there's nothing here"
>>> dic.get(99,None)
>>> dic.get(168,False)
False
>>> dic.get(20,"Boo Yah")
11
>>>

So a standard dictionary does this returning None (or any other default 
value you care to pass it) as long as you use the dict.get(key[,default]) 
method rather than dict[key] to return the value.

See also: 

http://stackoverflow.com/questions/6130768/return-none-if-dictionary-key-
is-not-available

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Creating a list with holes Larry Martell <larry.martell@gmail.com> - 2014-01-03 10:19 -0500
  Re: Creating a list with holes eneskristo@gmail.com - 2014-01-03 07:30 -0800
    Re: Creating a list with holes Larry Martell <larry.martell@gmail.com> - 2014-01-03 10:41 -0500
      Re: Creating a list with holes Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-03 18:07 +0000
        Re: Creating a list with holes Larry Martell <larry.martell@gmail.com> - 2014-01-03 19:15 -0500
          Re: Creating a list with holes Roy Smith <roy@panix.com> - 2014-01-03 20:18 -0500
            Re: Creating a list with holes Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-04 02:03 +0000
  Re: Creating a list with holes Roy Smith <roy@panix.com> - 2014-01-03 10:38 -0500
    Re: Creating a list with holes Chris Angelico <rosuav@gmail.com> - 2014-01-04 02:46 +1100

csiph-web