Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 5th, 2004, 06:45 PM
ntrian ntrian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 11 ntrian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 50 sec
Reputation Power: 0
simple list manipulation

i have a nested tuple/list such
a=[(1,2,3,4,5,6),(1,2,5,3,5,3),(2,4,5,3,4,3)]
i want to create out of it another list with the elements of the second column for example : b=[2,2,4] . Do you have any idea ?
i can just print such result but not as a new list


Nikos

Reply With Quote
  #2  
Old December 5th, 2004, 07:07 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,243 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 5 h 41 m 59 sec
Reputation Power: 265
List comprehensions to the rescue!

Code:
>>> a=[(1,2,3,4,5,6),(1,2,5,3,5,3),(2,4,5,3,4,3)]
>>> [x[1] for x in a]
[2, 2, 4]


There are other ways of doing it, but this is by far the simplest.

In Python 2.4 you can also use a generator expression to create a generator that will retrieve the results when needed. For a large list this can be much more efficient:

Code:
>>> gen = (x[1] for x in a)
>>> gen.next()
2
>>> gen.next()
2
>>> gen.next()
4
>>> gen.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
StopIteration
>>>


Dave - The Developers' Coach

Last edited by DevCoach : December 5th, 2004 at 07:13 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > simple list manipulation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT