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.

2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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']]

2 Comments

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.
Ooh thank you! I really liked that. Sorry for being nonexplanative. I done my best through the examples...
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)) ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.