3

This is my code, using Python 2.7

list1 = [1,2,3,4,5,6,9,10.....1000]

I'm trying to create chunks of 500 list data like this

chunk_data = [(1,2,3,4,5,...500),(501,502....1000)]

and then I'm going to add a "data" at the end of each chunk

so it will look like this

[(1,2...500,'data'),(501,502...1000,'data')]

I tried to use zip for this

it = iter(list1)    
chunked_data = zip(*[it]*500) 

but I'm unable to append the "data" now

from itertools import repeat
chunked_data = zip(*[it]*500,repeat("data")) #Gives Error about adding arguments after unpacking function values !!

It's not possible for me to write this code below even though it would work

chunked_data = zip(it,it,it...(500 times),repeat("data")]

So how should I attempt this?

wolfgang
  • 7,281
  • 12
  • 44
  • 72
  • Tuples are immutable, and can't be appended to. So, even if this code worked, it would need to copy and be very inefficient. If your goal is to re-use the list1 somehow, consider using a better data structure. If you don't intend to re-use it, consider using a loop that does something special when ((i+1) % 500 == 0) (or, more advanced, list comprehensions or generators). – David M. Rogers Jul 26 '15 at 15:40
  • @PadraicCunningham segregate the input array into chunks of 500 elements – wolfgang Jul 26 '15 at 15:42
  • what if the array is not evenly divisible by 500? – Padraic Cunningham Jul 26 '15 at 15:42
  • @PadraicCunningham That would'nt happen in the case of the input – wolfgang Jul 26 '15 at 15:48
  • This is a follow-on question to [Adding and creating chunks of a list in python](http://stackoverflow.com/q/31637486/4014959) – PM 2Ring Jul 26 '15 at 15:51
  • @wolfgang: Why do you like `zip` so much? What are the advantages I'm missing? (Disadvantages I can see: poor readability). – Tobia Tesan Jul 26 '15 at 16:03
  • @wolfgang: also, do you really *need* (immutable) tuples? You seem to be doing an awful lot of manipulations with those (see also: [other question](http://stackoverflow.com/q/31637486/4014959)) – Tobia Tesan Jul 26 '15 at 16:07

5 Answers5

3

You could create a list of positional arguments and then unpack it:

args = [it] * 500 + [repeat('data')]
chunked_data = zip(*args)

This is essentially a modified grouper recipe from the itertools documentation.

It works nicely:

In [17]: it = iter('0123456789')

In [18]: args = [it] * 5 + [repeat('data')]

In [19]: list(zip(*args))
Out[19]: [('0', '1', '2', '3', '4', 'data'), ('5', '6', '7', '8', '9', 'data')]

Alternative solution:

[tuple(list1[i:i+500]) + ('data',) for i in range(0, len(list1), 500)]

You can find more at this page.

Community
  • 1
  • 1
vaultah
  • 44,105
  • 12
  • 114
  • 143
2
[tuple(list1[i:i+500]+['data']) for i in xrange(0, len(list1), 500)]

You can change 500 and extend it for any chunk size

helloV
  • 50,176
  • 7
  • 137
  • 145
1

Function which provides your output

def chunks_with_data(l, n):
    return [tuple(l[i:i + n])+("data",) for i in range(0, len(l), n)]

>>>chunks_with_data(range(1000), 500)
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • This solution has come up an inordinate number of times, but +1 for being the first to parametrize `n`, which at this point seems a good idea. – Tobia Tesan Jul 26 '15 at 16:05
-1

But it might be very very long!!

base_lst = [x for x in range(1, 1001)]
lst = [tuple(base_lst[x:x+500]+['Data']) for x in range(500)]
print(lst)
Clodion
  • 1,017
  • 6
  • 12
  • Your response have one problem though. It would iterate over every element in the list and add the next 500 elements. What OP clearly wanted was getting tuples like (1...500), (501...1000), not (1...500), (2...501). – wswld Jul 26 '15 at 15:57
  • @wswld: Well… He said "chunks of 500 list data like this" So, for me it's 500 `tuple` in a `list`. No? – Clodion Jul 26 '15 at 16:00
  • It seems that he himself illustrated it like that: chunk_data = [(1,2,3,4,5,...500),(501,502....1000)]. – wswld Jul 26 '15 at 16:18
  • Yes, but it seems to that is easier to forget a comma: '[(),,()]' than to make an error like confounding "500 list" with "2 lists". No? – Clodion Jul 26 '15 at 16:24
  • And when he write: `it = iter(list1)` then `chunked_data = zip(*[it]*500)`, for me is to a 500 lists (here as an example). No? Don't you think so? – Clodion Jul 26 '15 at 16:33
  • Well, at this point it becomes unclear what OP really meant, as he illustrated it both ways. – wswld Jul 26 '15 at 16:40
-1

Here is my take on this problem. It returns 500 element chunks from a list ([(1...500),(501...1000)]) and appends data:

l = [x for x in range(1, 1001)]
out_l = [tuple(l[i:i+500] + ['data']) for i in xrange(0, len(l), 500)]
wswld
  • 1,218
  • 15
  • 32