simple question

I was wondering if there is a better way to write this codelet.
I have read that 'range(len(aList))' is poor form. ('Dive into Python', pg 107)

>>> li = [[1,1,'w'],[2,2,'e'],[3,3,'dddd']]

>>> q = [li[v][2] for v in range(len(li))]

>>> q

['w', 'e', 'dddd']

li could be a list of 1 item or 5,000 items, so I am looking for a faster way if one exists. q is what I am after.

Thanks
Scott

···

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005

In your example I would write;
li = [[1,1,'w'],[2,2,'e'],[3,3,'dddd']]
q = [x[2] for x in li]

···

-----Original Message-----
From: scott [mailto:scott@ebbyfish.com]
Sent: 05 April 2005 22:42
To: wxpython-users@lists.wxwidgets.org
Subject: [wxPython-users] simple question

I was wondering if there is a better way to write this codelet.
I have read that 'range(len(aList))' is poor form. ('Dive into Python',
pg 107)

>>> li = [[1,1,'w'],[2,2,'e'],[3,3,'dddd']]

>>> q = [li[v][2] for v in range(len(li))]

>>> q

['w', 'e', 'dddd']

li could be a list of 1 item or 5,000 items, so I am looking for a
faster way if one exists. q is what I am after.

Thanks
Scott

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Ah, the obvious! Thank you so much!
:slight_smile:

Scott

Nikolai Kirsebom wrote:

···

In your example I would write;
li = [[1,1,'w'],[2,2,'e'],[3,3,'dddd']]
q = [x[2] for x in li]

-----Original Message-----
From: scott [mailto:scott@ebbyfish.com] Sent: 05 April 2005 22:42
To: wxpython-users@lists.wxwidgets.org
Subject: [wxPython-users] simple question

I was wondering if there is a better way to write this codelet.
I have read that 'range(len(aList))' is poor form. ('Dive into Python', pg 107)

>>> li = [[1,1,'w'],[2,2,'e'],[3,3,'dddd']]

>>> q = [li[v][2] for v in range(len(li))]

>>> q

['w', 'e', 'dddd']

li could be a list of 1 item or 5,000 items, so I am looking for a faster way if one exists. q is what I am after.

Thanks
Scott

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005