What's correct Python syntax?

Roy Smith roy at panix.com
Tue Jan 14 08:47:35 EST 2014


In article <mailman.5443.1389693753.18130.python-list at python.org>,
 Igor Korot <ikorot01 at gmail.com> wrote:

> I can do it this way:
> 
> >>> testlist = test.split(',')
> >>> print testlist[2]
> my
> 
> but it will needlessly creates a list on which I will access by the index.

Stop worrying about needlessly creating lists.  Write the code in a way 
that works and is easy to understand.  If it turns out that it's not 
running fast enough, then you can go back and optimize.

BTW, for those of you into code golf:

>>> line = '192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 
200, length 30'

>>> dict((k,int(v)) for k,v in (s.split() for s in line.split(', ')[1:]))
{'length': 30, 'id': 100, 'seq': 200}



More information about the Python-list mailing list