0

I have the list as below :

item = ['3138591780' ,'3145056746' ,'3228083330' ,'3230089832' ,'3238650148' ,'3241690762' ,
         '3257712043' ,'3754126989' ,'3799112085' ,'3801697389' ,'3801701129' ,'3801708081']

I want to pick the 3 items from this iterative manner so in first iteration it will return sublist as ['3138591780' ,'3145056746' ,'3228083330'] . 2nd iteration it will return ['3230089832' ,'3238650148' ,'3241690762'] How can I achieve this ?

thanks

pauldx
  • 833
  • 1
  • 12
  • 22

1 Answers1

1

This should be quite simple:

item = ['3138591780' ,'3145056746' ,'3228083330' ,'3230089832' ,'3238650148' ,'3241690762' ,
         '3257712043' ,'3754126989' ,'3799112085' ,'3801697389' ,'3801701129' ,'3801708081']

while len(item) > 0:
    if len(item) >= 3:
        slice_size = 3
    else:
        slice_size = len(item)
    slice = item[:slice_size]
    for elem in slice:
        item.remove(elem)
    print (slice)
Marco
  • 1,952
  • 1
  • 17
  • 23
  • Thank you, slice did the trick . But only caveats I am seeing is that if I have 11 items then last 2 is not derived . Anyway to handle this as well ? – pauldx Dec 31 '19 at 18:19
  • This should work! – Marco Dec 31 '19 at 18:38
  • do you have idea rather printing the slice how can I get each slice in separate list that means slice[1] will be showing 3 items and slice[2] will be showing next 3 ? and that was the actual intent to split into separate list – pauldx Dec 31 '19 at 19:58