[BangPypers] Iterating list of tuples

delegbede at dudupay.com delegbede at dudupay.com
Mon Jul 4 08:37:10 CEST 2011


Asif,

You can iterate over a tuple. When doing that you reference the values directly. 
From your example, data is a list containing a tuple. To check this, do this from the prompt. 
type(data)
You should get:
<Type 'list'>

That said, your mistake is
On the if line. It should read:

 if value > 5:
And not what you wrote. 

For the purpose of another time, kindly paste the error message. With this we can point you to the direction of error. 

Also, indent he print command after the else: properly. 

Your new code should read this. 

data = [
    (10, 25, 18, 17, 10, 12, 26, 5),
    ]

for value in data:
     if value > 5:
                print " greater"
    else:
                print "lesser"


Sent from my BlackBerry wireless device from MTN

-----Original Message-----
From: Asif Jamadar <asif.jamadar at rezayat.net>
Sender: bangpypers-bounces+delegbede=dudupay.com at python.org
Date: Mon, 4 Jul 2011 06:24:48 
To: bangpypers at python.org<bangpypers at python.org>
Reply-To: Bangalore Python Users Group - India <bangpypers at python.org>
Subject: [BangPypers] Iterating list of tuples

Suppose I have list of tuples

data = [
    (10, 25, 18, 17, 10, 12, 26, 5),
    ]

for value in data:
     if data[value]>5:
                print " greater"
    else:
       print "lesser"

But the code giving me error so can I know how iterate list of tuples?

_______________________________________________
BangPypers mailing list
BangPypers at python.org
http://mail.python.org/mailman/listinfo/bangpypers


More information about the BangPypers mailing list