how can I avoid abusing lists?

Ant antroy at gmail.com
Fri Jul 7 12:44:44 EDT 2006


Rob Cowie wrote:
> Just forget the lists...
> counters = {0:0, 1:0, 2:0, 3:0, 4:0}

Or perhaps just use a list:

>>> counters = [0,0,0,0]
>>> def inc(v):
...   counters[v] += 1
...
>>> inc(1)
>>> inc(1)
>>> inc(3)
>>> counters
[0, 2, 0, 1]

> The increment function should probably include a try:...except:

Likewise for IndexErrors




More information about the Python-list mailing list