For Loop in List

Discussion in 'Python' started by subhabangalore@gmail.com, Jan 13, 2013.

  1. Guest

    Dear Group,

    I have a list like,

    >>> list1=[1,2,3,4,5,6,7,8,9,10,11,12]


    Now, if I want to take a slice of it, I can.
    It may be done in,
    >>> list2=list1[:3]
    >>> print list2

    [1, 2, 3]

    If I want to iterate the list, I may do as,

    >>> for i in list1:

    print "Iterated Value Is:",i


    Iterated Value Is: 1
    Iterated Value Is: 2
    Iterated Value Is: 3
    Iterated Value Is: 4
    Iterated Value Is: 5
    Iterated Value Is: 6
    Iterated Value Is: 7
    Iterated Value Is: 8
    Iterated Value Is: 9
    Iterated Value Is: 10
    Iterated Value Is: 11
    Iterated Value Is: 12

    Now, I want to combine iterator with a slicing condition like

    >>> for i=list2 in list1:

    print "Iterated Value Is:",i

    So, that I get the list in the slices like,
    [1,2,3]
    [4,5,6]
    [7,8,9]
    [10,11,12]

    But if I do this I get a Syntax Error, is there a solution?

    If anyone of the learned members may kindly let me know?

    Apology for any indentation error,etc.

    Thanking You in Advance,

    Regards,
    Subhabrata
    , Jan 13, 2013
    #1
    1. Advertising

  2. Dave Angel Guest

    On 01/13/2013 07:45 AM, wrote:
    > Dear Group,
    >
    > I have a list like,
    >
    >>>> list1=[1,2,3,4,5,6,7,8,9,10,11,12]


    What version of Python?

    > Now, if I want to take a slice of it, I can.
    > It may be done in,
    >>>> list2=list1[:3]
    >>>> print list2

    > [1, 2, 3]
    >
    > If I want to iterate the list, I may do as,
    >
    >>>> for i in list1:

    > print "Iterated Value Is:",i
    >
    >
    > Iterated Value Is: 1
    > Iterated Value Is: 2
    > Iterated Value Is: 3
    > Iterated Value Is: 4
    > Iterated Value Is: 5
    > Iterated Value Is: 6
    > Iterated Value Is: 7
    > Iterated Value Is: 8
    > Iterated Value Is: 9
    > Iterated Value Is: 10
    > Iterated Value Is: 11
    > Iterated Value Is: 12
    >
    > Now, I want to combine iterator with a slicing condition like
    >
    >>>> for i=list2 in list1:

    > print "Iterated Value Is:",i
    >
    > So, that I get the list in the slices like,
    > [1,2,3]
    > [4,5,6]
    > [7,8,9]
    > [10,11,12]
    >
    > But if I do this I get a Syntax Error, is there a solution?


    It'd be only polite if you actually included the traceback, instead of
    paraphrasing the error.

    > If anyone of the learned members may kindly let me know?
    >
    > Apology for any indentation error,etc.
    >
    > Thanking You in Advance,
    >
    > Regards,
    > Subhabrata
    >
    >
    >
    >


    let's examine the code that generates the syntax error.
    for i=list2 in list1:

    That doesn't match any of the grammar of Python, so it gives a syntax
    error. How could the compiler have interpreted it? Perhaps it could
    have thrown out the 'for' and the colon. That would be equivalent in
    this case to:
    i = False

    or we could toss out the "=list2" but that would give us your first loop.

    If I were doing this, I'd do something like (untested):

    temp = list1[:] #make a shallow copy of the list, so we're not
    modifying the original
    while temp
    print temp[:3]
    temp = temp[3:]

    I think you could do something similar with zip, but I don't have the
    energy this morning.



    --

    DaveA
    Dave Angel, Jan 13, 2013
    #2
    1. Advertising

  3. Tim Chase Guest

    On 01/13/13 06:45, wrote:
    > Dear Group,
    >
    > I have a list like,
    >
    >>>> list1=[1,2,3,4,5,6,7,8,9,10,11,12]

    >
    > Now, if I want to take a slice of it, I can.
    > It may be done in,
    >>>> list2=list1[:3]
    >>>> print list2

    [snip]
    > Now, I want to combine iterator with a slicing condition like
    >
    >>>> for i=list2 in list1:

    > print "Iterated Value Is:",i
    >
    > So, that I get the list in the slices like,
    > [1,2,3]
    > [4,5,6]
    > [7,8,9]
    > [10,11,12]


    Well, you get a SyntaxError because, well, it's a syntax error. It
    may take a little math to do this:

    >>> SIZE = 3
    >>> for i in range(len(list1)//SICE):

    .... print list1[i*SIZE:i*SIZE+SIZE]
    ....
    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    [10, 11, 12]

    Or, you can exploit the fact that iterators exhaust inside a for-loop:

    >>> i = iter(list1)
    >>> for item in i:

    .... print [item, i.next(), i.next()]
    ....
    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    [10, 11, 12]



    Hope this helps,

    -tkc
    Tim Chase, Jan 13, 2013
    #3
  4. Boris FELD Guest

    2013/1/13 Tim Chase <>:
    > On 01/13/13 06:45, wrote:
    >
    >>>> SIZE = 3
    >>>> for i in range(len(list1)//SICE):

    > ... print list1[i*SIZE:i*SIZE+SIZE]
    > ...
    > [1, 2, 3]
    > [4, 5, 6]
    > [7, 8, 9]
    > [10, 11, 12]
    >


    A little shorter and simpler version:
    >>> x = x[1:]
    >>> for i in range(0,len(x),SIZE):

    .... print x[i: i+SIZE]
    ....
    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    [10, 11, 12]

    Hope it helps

    > Hope this helps,
    >
    > -tkc
    Boris FELD, Jan 13, 2013
    #4
  5. Tim Chase Guest

    On 01/13/13 07:48, Boris FELD wrote:
    > 2013/1/13 Tim Chase <>:
    >>>>> SIZE = 3
    >>>>> for i in range(len(list1)//SICE):

    >> ... print list1[i*SIZE:i*SIZE+SIZE]

    >
    > A little shorter and simpler version:
    >>>> x = x[1:]
    >>>> for i in range(0,len(x),SIZE):

    > ... print x[i: i+SIZE]


    Doh, I always forget that range() takes an optional stride. Or
    rather, I use it so infrequently that I reach for other alternatives
    before it occurs to me :)

    -tkc
    Tim Chase, Jan 13, 2013
    #5
  6. On 01/13/2013 07:45 AM, wrote:
    > Dear Group,
    >
    > I have a list like,
    >
    >>>> list1=[1,2,3,4,5,6,7,8,9,10,11,12]

    > Now, if I want to take a slice of it, I can.
    > It may be done in,
    >>>> list2=list1[:3]
    >>>> print list2

    > [1, 2, 3]
    >
    > If I want to iterate the list, I may do as,
    >
    >>>> for i in list1:

    > print "Iterated Value Is:",i
    >
    >
    > Iterated Value Is: 1
    > Iterated Value Is: 2
    > Iterated Value Is: 3
    > Iterated Value Is: 4
    > Iterated Value Is: 5
    > Iterated Value Is: 6
    > Iterated Value Is: 7
    > Iterated Value Is: 8
    > Iterated Value Is: 9
    > Iterated Value Is: 10
    > Iterated Value Is: 11
    > Iterated Value Is: 12
    >
    > Now, I want to combine iterator with a slicing condition like
    >
    >>>> for i=list2 in list1:

    > print "Iterated Value Is:",i
    >
    > So, that I get the list in the slices like,
    > [1,2,3]
    > [4,5,6]
    > [7,8,9]
    > [10,11,12]
    >
    > But if I do this I get a Syntax Error, is there a solution?
    >
    > If anyone of the learned members may kindly let me know?
    >
    > Apology for any indentation error,etc.
    >
    > Thanking You in Advance,
    >
    > Regards,
    > Subhabrata
    >
    >
    >
    >



    Another good answer is to use a recipe from itertools docs page.
    There are a lot of good recipes there and you may want to keep
    them all in a module you can import from when needed. Here
    is the recipe:

    def grouper(n, iterable, fillvalue=None):
    """From itertools recipes: collect data into fixed-length chunks or
    blocks."""
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return zip_longest(fillvalue=fillvalue, *args)

    >>> list(grouper(3, range(12))

    ....
    ....
    ....
    .... )
    [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]


    HTH, - mitya


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/
    Mitya Sirenef, Jan 13, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. author
    Replies:
    5
    Views:
    616
    author
    Jul 28, 2004
  2. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,377
    Karl Seguin
    Feb 16, 2005
  3. Replies:
    5
    Views:
    595
    benben
    Jan 31, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    423
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page