list previous or following list elements

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Jun 27 04:18:05 EDT 2008


Terry Reedy a écrit :
(snip)
> I believe
> wordlist = open('words.txt','r').read().split('\n')
> should give you the list in Python.  

Or simply:
wordlist = open('words.txt').readlines()

>In any case,
> 
> wordlist = ['Apple','balcony', 'cartridge',
> 'damned', 'paper', 'bold', 'typewriter']
> for i, word in enumerate(wordlist):
>     if word[0] == 'b':

While this is perfectly legal, I'd rather use word.startswith('b') here. 
But don't ask me why !-)

>         print(wordlist[i+1]) 

Hmmm... Guess what will happen if wordlist == ['Apple', 'balcony'] ?






More information about the Python-list mailing list