chowpay
chowpay

Reputation: 1687

how to convert a list of arrays to a python list

Given a number I want to split it separate but even-ish list which I can do :

import numpy as np
pages = 7
threads = 3
list_of_pages = range(1,pages+1)
page_list = [*np.array_split(list_of_pages, threads)]

Returns:

[array([1, 2, 3]), array([4, 5]), array([6, 7])]

I would like it to return a list of lists instead, ie:

[[1,2,3],[4,5],[6,7]]

I was hoping to do something like this (below doesnt work):

page_list = np.array[*np.array_split(list_of_pages, threads)].tolist()

is that possible or do I need to just loop through and convert it?

Upvotes: 2

Views: 1691

Answers (2)

Nautatava Navlakha
Nautatava Navlakha

Reputation: 315

@zihaozhihao gave a inline solution. You can also write a for loop to iterate over every item in page_list and convert it to list

list_of_lists = [ ]

    for x in [*page_list]]:
        list_of_lists.append(x.tolist())

Upvotes: 1

zihaozhihao
zihaozhihao

Reputation: 4495

Assuming page_list is a list of ndarray, then you can convert to python list like this,

[x.tolist() for x in [*page_list]]
# [[1, 2, 3], [4, 5], [6, 7]]

Upvotes: 4

Related Questions