11

I am trying to come up with a function that takes an input x and split a big list with the number of elements x*x into x smaller lists with x elements in every list E.g:

big_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

def split_list (x):
big_list = pairs (x)
small_list = [big_list[0:x] for x in range (x)]

My output has to be:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] 

but I'm not getting it, what do you recommend ?

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
nazeeroo bu
  • 177
  • 1
  • 9

5 Answers5

12

You can try this:

big_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

def split_list (x):
   return [big_list[i:i+x] for i in range(0, len(big_list), x)]

print(split_list(4))

Output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
5

I use this code all the time.

def chunkify(items, chunk_len):
    return [items[i:i+chunk_len] for i in range(0,len(items),chunk_len)]
Evan
  • 2,120
  • 1
  • 15
  • 20
3

Here's one solution:

def split_list(big_list, x):
    list_size = len(big_list)
    splits = int(list_size / x)

    return [big_list[k*x:k*x+x] for k in range(splits)]

big_list = [i+1 for i in range(16)]
print(big_list)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

small_list = split_list(big_list, 4)
print(small_list)
# [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]


small_list = split_list(big_list, 2)
print(small_list)
# [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], [15, 16]]

small_list = split_list(big_list, 3)
print(small_list)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]
Arthur Gouveia
  • 734
  • 4
  • 12
2

First you want to get the size using square root, so you for a list of size n you would have an m x m matrix with m = n ** 0.5. Let's first define your function:

def square(array):
    n = len(array)
    m = int(n ** 0.5)

Since list sizes have to be integers, we have to call int on the result. Next, we want to start from 0 and count i up to n by m each time, taking m elements starting from i:

def square(array):
    n = len(array)
    m = int(n ** 0.5)
    result = []
    for i in range(0, n, m):
        result.append(array[i:i + m])
    return result

And that's it.

Alternatively, s=lambda a:(lambda m:[a[i*m:i*m+m]for i in range(m)])(int(len(a)**.5))

hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
1

why not use numpy :

>>> import numpy as np
>>> big_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> a=np.array(big_list)
>>> a
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])
>>> n=int(len(big_list)**0.5)
>>> a.reshape(n,n)
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])

or just :

>>> big_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> n=int(len(big_list)**0.5)
>>> new_list=[big_list[i:i+n] for i in range(0, len(big_list), n)]
>>> new_list
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Dadep
  • 2,796
  • 5
  • 27
  • 40