How do I pass a list to a __init__ value/definition?

Bruno Desthuilliers onurb at xiludom.gro
Tue Jul 25 10:03:51 EDT 2006


ryanshewcraft at gmail.com wrote:
> Bruno may be right.  At some point I've got
> 
> del self.regressors[fundNumber]

oops... You're probably in for troubles with this:
>>> l = [1, 2, 3, 4]
>>> del l[1]
>>> l
[1, 3, 4]
>>> del l[1]
>>> l
[1, 4]
>>> del l[1]
>>> l
[1]
>>> del l[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
>>>

You perhaps want a dict instead:
>>> l = [1, 2, 3, 4]
>>> d = dict(zip(l, l))
>>> d
{1: 1, 2: 2, 3: 3, 4: 4}
>>> del l[1]
>>> l
[1, 3, 4]
>>> d
{1: 1, 2: 2, 3: 3, 4: 4}
>>> del d[1]
>>> d
{2: 2, 3: 3, 4: 4}
>>> del d[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 1
>>> del d[4]
>>> d
{2: 2, 3: 3}
>>>

> which eliminates one of the variables in the list. 

This *won't* turn the list into an integer:
>>> l = [1, 2, 3, 4]
>>> while l:
...     del l[0]
...
>>> l
[]
>>> for item in l: print item
...
>>>



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list