0

I m new to python , I have looping issue to get chuck of data for a list.

I have large list where I need to use chunk of it until it becomes entirely nil.

Lets say I have list as :

a = range(4000)  # range 100 -9k
n = 99

while a:
    x = a[:n]   # want to use first 100 elements 
    some insertion work of (x) in dB
    a = a[n+1 :]  reducing first 100 elements from main list

but this method is not working .

Can anybody suggest me a proper approach for this.

Thanks

  • this answer https://stackoverflow.com/a/312464/4916954 shows how to divide a list into chunks of specific lenght, if that is what you're after – Atn Jul 18 '17 at 16:41
  • @Atn its not about just slicing.I want to reduce the list as I use this chunks –  Jul 19 '17 at 06:04
  • Is it the slice notation you have troubles with? replacing n= 99 by n=100 and a =a[n+1:] by a[n:] should fix your issue – Atn Jul 19 '17 at 07:58

1 Answers1

0

a[:n] when n is 99 gets the first 99 elements - so change n to 100.

a = a[n+1:] will miss an element - so change n+1 to n

The full code:

a = range(4000)
n = 100

while a:
    x = a[:n]
    # some insertion work of (x) in dB
    a = a[n:] # reducing first 100 elements from main list
AbyxDev
  • 1,363
  • 16
  • 30