[Tutor] working with empty lists

Timo timomlists at gmail.com
Thu Sep 16 09:18:58 CEST 2010


On 16-09-10 06:05, Rance Hall wrote:
> Im working on a little piece of test code to test an idea for a larger script.
>
> Here is what I've got so far:
>
> l = []
>
> for i in range(0,10)
>      l.append(i)
>
> for i in range(0,10)
>      print('%s. %s' % (i, l[i])
>
>
> This gives me:
>
> 0. 0
> 1. 1
> 2. 2 .... etc
>
> which is what I expect, but what I want is to get something like
>
> 0. 1
> 1. 2
> 2. 3 .....
>
> so that the output is clearly different on either side of the "."
>    
You could do this:
 >>> l = []
 >>> for i in range(1, 11):
...   l.append(i)
...
 >>> for index, i in enumerate(l):
...   print('%s. %s' %(index, i))
...
0. 1
1. 2
2. 3
3. 4
etc.

Cheers,
Timo

> I tried changing the append to l.append(i+1)
>
> which almost worked but the output started with 1. 2  I was curious
> what happend to the 0. 1 line
>
> I know this means that I'm not understanding exactly what append actually does.
>
> I know that my ide shows that the list as other functions like insert, etc.
>
> Can someone explain to me whats the best way to add a value to a list
> that is not long enough to accept it, without playing with the
> indexes, it appears I currently am playing with them.
>
> I know generally that we aren't supposed to care about the indexes but
> this is eventually going to be part of a menuing system that displays
> the index, so I do have a legit need to care about what is happening
> to the list index.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>    



More information about the Tutor mailing list