5
a = [3,5,8,3,9,5,0,3,2,7,5,4]
for o in a[::3]:
    print o

This gets me the first and every 3 item. 3,3,0,7

is there a way i can retrieve the next two items as well?

a = [3,5,8,3,9,5,0,3,2,7,5,4]
for o in a[::3]:
  if o == 0:
    print o
    print o + 1
    print o + 2

output 0 3 2

I know that's not correct but maybe you can see what I'm trying to do. Basically, I have a long list of properties and there are three parts to each property, parent_id, property_type and property_value and I need to retrieve all three parts of the property from the list.

Coo Jinx
  • 339
  • 1
  • 4
  • 4
  • 2
    How do you put the properties *into* your list? Could you add them as a tuple, ie your list would look like `[(3,5,8), (3,9,5), (0,3,2), (7,5,4)]`? – Hugh Bothwell Jun 11 '12 at 22:28
  • Along the lines of what @Hugh Bothwell said: In Python a `property` is a type and you can derive your own subclass from it which has its own unique "parts" (aka attributes), yet can usually be otherwise used just like the built-in one. – martineau Jun 12 '12 at 01:34
  • The question [How do I dynamically create properties in Python?](http://stackoverflow.com/questions/10967551/how-do-i-dynamically-create-properties-in-python) might be useful and eliminate your need to do this. – martineau Jun 12 '12 at 19:55

6 Answers6

14

You can do this using the "grouper" recipe from the itertools documentation:

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Example:

>>> a = [3, 5, 8, 3, 9, 5, 0, 3, 2, 7, 5, 4]
>>>  list(grouper(3, a))
[(3, 5, 8), (3, 9, 5), (0, 3, 2), (7, 5, 4)]
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 3
    Damn the itertools! :) They are just awesome. – Zaur Nasibov Jun 11 '12 at 21:54
  • It's always amazing to me when someone gets a lot of up-votes just for pointing out what's in the documentation...including [myself](http://stackoverflow.com/a/3909907/355230). – martineau Jun 12 '12 at 01:18
3

Try this:

array = [3,5,8,3,9,5,0,3,2,7,5,4]
for i in xrange(0, len(array), 3):
    a, b, c = array[i:i+3] # partition the array in groups of 3 elements
    print a, b, c

It works because (as stated in the question) there are exactly three parts to each property.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
3

If you know there are exactly three parts to each one, how about simply:

a = [3,5,8,3,9,5,0,3,2,7,5,4]

print zip(*[iter(a)] * 3)

Output:

[(3, 5, 8), (3, 9, 5), (0, 3, 2), (7, 5, 4)]

It's from this answer.

Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301
2
>>> a = [3,5,8,3,9,5,0,3,2,7,5,4]
>>> for pos in xrange(0, len(a), 3):
...     print a[pos:pos+3]
... 
[3, 5, 8]
[3, 9, 5]
[0, 3, 2]
[7, 5, 4]
>>> 
ddzialak
  • 1,042
  • 7
  • 15
2

Not very pythonic, but you could use:

for i in range(0, len(a), 3):
    print a[i]
    print a[i+1]
    print a[i+2]
dckrooney
  • 3,041
  • 3
  • 22
  • 28
1
x = a[::3]
y = a[1::3]
z = a[2::3]
grouped = zip(x,y,z)

for p1,p2,p3 in grouped:
    print p1 + p2 + p3
Hans Z
  • 4,664
  • 2
  • 27
  • 50
  • I suppose, but you're turning the data structure into something that more accurately reflects the underlying structure of your data. – Hans Z Jun 11 '12 at 22:03