0

I done the following Python script which should return a list of sublists.

def checklisting(inputlist, repts):
result = []
temprs = []
ic = 1;
for x in inputlist
    temprs.append(x)
    ic += 1
    if ic == repts:
        ic = 1
        result.append(temprs)
return result

Example: If I called the function with the following arguments:

checklisting(['a', 'b', 'c', 'd'], 2)

it would return

[['a', 'b'], ['c', 'd']]

or if I called it like:

checklisting(['a', 'b', 'c', 'd'], 4)

it would return

[['a', 'b', 'c', 'd']]

However what it returns is a weird huge list:

    >>> l.checklisting(['a','b','c','d'], 2)
[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']]

Someone please help! I need that script to compile a list with the data:

['water tax', 20, 'per month', 'electric tax', 1, 'per day']

The logic behind it is that it would separe sequences in the list the size of repts into sublists so it can be better and easier organized. I don't want arbitrary chunks of sublists as these in the other question don't specify the size of the sequence correctly.

wallabra
  • 412
  • 8
  • 17
  • Possible duplicate of [How do you split a list into evenly sized chunks in Python?](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) – TigerhawkT3 Oct 10 '15 at 00:05
  • it didn't worked for me :( – wallabra Oct 10 '15 at 11:55

3 Answers3

2

Your logic is flawed.

Here are the bugs: You keep appending to temprs. Once repts is reached, you need to remove elements from temprs. Also, list indexes start at 0 so ic should be 0 instead of 1

Replace your def with:

def checklisting(inputlist, repts):
    result = []
    temprs = []
    ic = 0;
    for x in inputlist:
        temprs.append(x)
        ic += 1
        if ic == repts:
            ic = 0
            result.append(temprs)
            temprs = []

    return result

Here is link to working demo of code above

sam
  • 2,033
  • 2
  • 10
  • 13
1
def split_into_sublists(list_, size):
    return list(map(list,zip(*[iter(list_)]*size)))

    #[iter(list_)]*size this creates size time lists, if 
    #size is 3 three lists will be created.
    #zip will zip the lists into tuples
    #map will covert tuples to lists.
    #list will convert map object to list.

print(split_into_sublists(['a', 'b', 'c', 'd'], 2))

    [['a', 'b'], ['c', 'd']]

print(split_into_sublists(['a', 'b', 'c', 'd'], 4))

[['a', 'b', 'c', 'd']]
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
  • While this answer may be correct, please add some explanation. Imparting the underlying logic is more important than just giving the code, because it helps the OP and other readers fix this and similar issues themselves. – CodeMouse92 Oct 10 '15 at 01:04
  • Ooh thank you! I really liked that. Sorry for being nonexplanative. I done my best through the examples... – wallabra Oct 10 '15 at 11:50
0

I got lost in your code. I think the more Pythonic approach is to slice the list. And I can never resist list comprehensions.

def checklisting(inputlist, repts):
    return [ input_list[i:i+repts] for i in range(int(len(input_list)/repts)) ]
RobertB
  • 1,879
  • 10
  • 17