[BangPypers] Iterating list of tuples
Anand Balachandran Pillai
abpillai at gmail.com
Mon Jul 4 09:17:09 CEST 2011
On Mon, Jul 4, 2011 at 12:01 PM, Noufal Ibrahim <noufal at gmail.com> wrote:
> Asif Jamadar <asif.jamadar at rezayat.net> writes:
>
> > 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"
>
>
> if the list has just one tuple, you need to iterate over it's individual
> elements.
>
> for i in data[0]: # Iterate over elements of the tuple
> if i > 5:
> print "greater"
> else:
> print "lesser"
>
> `value` in your code does not mean the index, it's the actual element
> itself.
>
This is the correct approach. If you don't like
doing it like this, there is a 2nd approach where
you can index inside the loop rather than "on" the
loop
>>> data=[(10,25, 18, 17, 10, 12, 26, 5)]
>>> for value in zip(*data):
... if (value[0] > 5): print 'Greater'
... else: print 'Lesser'
...
Greater
Greater
Greater
Greater
Greater
Greater
Greater
Lesser
But then I might have confused you with that !
>
> [...]
>
>
> --
> ~noufal
> http://nibrahim.net.in
>
> I'm proud of my humility.
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
--
--Anand
More information about the BangPypers
mailing list