creating a numpy array from lists with list names | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

creating a numpy array from lists with list names

Hi, I would like to create a list of lists (numpy array) with list names, mean and list length. Example: list1 = [1,2,3] list2 = [4,5,6,7,8] list3 = [7,8,9,10,11,12,13] Code: list_of_lists = [list1, list2, list3] final_numpy = ['list_name', 'mean', 'list length'] for each in list_of_lists: final_numpy.append([..., mean(each), len(each)]) I would like to have an output like this: final_numpy = [['list_name', 'mean', 'list length'], ['list1', 2, 3], ['list2', 6, 5], ['list3', 10, 7]] I am almost there but don't know how to include the list name in the loop. please help

19th Aug 2019, 12:21 PM
Marek Kluczewski
Marek Kluczewski - avatar
5 Answers
+ 2
+Marek Kluczewski i literally re-wrote it for you already lol
19th Aug 2019, 2:21 PM
haydenki
haydenki - avatar
+ 2
Thank you for your help. I have a question though. Is there an automated way to include list names as strings? I thought that maybe there is a function which can get the list name from the list while looping through various it.
19th Aug 2019, 2:26 PM
Marek Kluczewski
Marek Kluczewski - avatar
+ 2
+Marek Kluczewski It's not possible. Names hold references to memory, but memory does not hold references to names which reference them.
19th Aug 2019, 3:22 PM
haydenki
haydenki - avatar
+ 1
Here i rewrote the code for you and it's working :-) https://code.sololearn.com/cvjWaP1UO3M9/#py The problem is when you have the name of a list in another list, the list item is replaced with the contents of the list. For example, print(list_of_lists[list1]) is the same as: print(list1)
19th Aug 2019, 1:28 PM
haydenki
haydenki - avatar
- 1
What do you mean exactly? I haven't tried it
19th Aug 2019, 1:02 PM
Marek Kluczewski
Marek Kluczewski - avatar