A guide to manipulate a list of lists (...of lists)

Downloads
The source file ListOfLists.aia (32.0 KB)
The apk file ListOfLists.apk (5.3 MB)
The text file AllData.txt (752 Bytes)
This guide in pdf format ListOf3Levels.pdf (1.3 MB)

Credits
To Kodular for the IDE.
To @yusufcihan for his extension, along with many explanations in his topic.

Kodular platform contains a component, named List, which handles data needed by the app. A list might consist of numbers, strings, colors… Could a list include other lists as elements? The answer is yes. Here, I will try to explain some principles of how to create and manage elements of lists which are elements of another list.

Suppose you own a school which offers courses in Math, Physics, Spanish and Art. A number of students attend lessons enrolled in one of them. Exams are held quarterly a year. You keep records of students as Name, their grades for 3 exams in each course and a note whether a student has got a scholarship or not.

So, the general view of our project is like that:


The first list represents Courses (level 1, green). Each node gives the base to construct a second list (level 2, orange) containing Students. Then, each Student is the base to build a third list (level 3, blue) with attributes of each person.

In the picture are shown 3 students (Mary, Peter, Bob) indexed as 8, 9, 6 respectively. Of course, there are other students in lower indices, not shown for simplicity of the design.

Note that areas marked with X are elements of the relevant list, but they do NOT contain data as blue areas do (“Mary”, “85”, “yes”). Each one of X ’s is the head (the node) for the list that follows.

Another note to mention is that length of lists of level 2 (number of students in each course) need not be the same.

Construct variables

We name ListOfAllData the list containing all our data. This list has the base points for 4 children lists which will serve as Courses.
b01

As heading points ( X ) do not contain data, we create another list, ListOfCourseNames to associate with the main list (must be of equal length). We shall use it later at “Clicking [ Find min avg schlr ] button”.
b02

We also need a third list indicating components that will accept dynamic labels. It is also associated with the main list. See later at “Clicking [ Show all ] button”.
b07

Finally, a list will accept lines (records) of a txt file. See how at “Clicking [ Load data ] button”.
b08

Add elements

Now, let’s add some elements in the main list when initializing.


Main list has 4 elements dedicated to Courses. Courses 1 and 4 of the main list have no elements (Students). Now, Course 2 has one student (a sub list) of 5 elements (a sub sub-list with elements Alice, 72, 82, 67, false), while Course 3 has one element for a future student, not assigned for the moment.
An advantage of lists is that can hold variables of different type.
The basic rule to get or set the value of an element of a multi-level list is built blocks containing indices which correspond to the list levels in descending order (see picture above).

Further examination – How to reach elements

Now, we can explore more opportunities about this example by the aid of this app.

Clicking [ Load data ] button we import data of 31 students split down in courses along with their attributes. Data file is AllData.txt packaged within the app. See blocks below of how information is set.


Clicking [ Find max in Math ] button we scan all data of Math and we locate the student who got the maximum grade.

Clicking [ Find min avg schlr ] button we locate the student who has got the minimum average among those granted a scholarship.

Clicking [ Show all ] button we get a list of all students distributed in separated Vertical Scroll Arrangements according to their Course with Name, score exams and status of scholarship.

As lists are dynamic components, we cannot easily set a certain number of static labels in Designer mode. Here we need the extension of @yusufcihan named [F/OS] - Dynamic Components Extension (for every component) 2.0.

Now, suppose a mistake has occurred in 3rd exam of Physics, so in re-evaluation of grades a bonus of 5 points must be given to all students of that Course. The access of such elements is done as follows (to implement this piece of code you have to include it in an event).

As last item in this guide let’s give a more sophisticated outlook in [ Show all ] display. Wouldn’t be nice to have alternatively tinted colors for each student in his/her course?

We create a two-level list, ListOfColours, which at first level has four elements as Courses are, while at second level there are 2 elements for the tint.

Then, we have to add next blocks at the btShowAll.Click event.

Try your own projects now.

23 Likes

Great and interesting work with multi-dimensional lists.

1 Like

thank you, you put a lot of work into not only this guide, but also the example app…

in the end the conclusion probably may be to prefer the local SQLite database, which would make things a lot simpler…

Taifun

4 Likes

Great guide!
I’ve never known you can add item to list of list just by selecting the list in list. Maybe that’s just not working for me.

I will have to get the list and assign to a local variable, add items to that variable and finally replace the item in the original variable.

1 Like

Great :hushed:

Your efforts for making the guide is appricated :clap:

![|500x278](upload://10hEQoZML5DwMIUXhVxS9YNA2th.mp4)
1 Like

A great and a complete guide

1 Like

Thank you for your comments and the time spent over this post. Of course the app could be implemented easier by other techniques. I tried to give an example, complete and simplified, just to show how one can use properties of lists in higher level.
As next step I will try SQLite in depth.

With SQL conmands and Relational databases, your work will pay off. Your work was great, if it was done with relational databases + SQL you would have less work. I have a database of exactly school. Students, School Disciplines, Courses, Classes, Teachers, Notes … in DBF and MySQL.
Operating for 15 years.

1 Like

I would love to learn more about list of lists, if you can send articles, videos about it I’m grateful.

The guide above is complete. It explains in detail how we can make and manipulate. It serves as your base of studies. Design a project that requires lists and sublists. Try to do using this guide

Thank for a great tutorial

After this tutorial i started using the sublist into sublist concept for easy data formation

I am fasing a issue. I want to store the data of a global variable which has gone to third stage of list that is “data in list which is sublist of sublist of list” into tinydb and then convert that tinydb to file for storing data offline. How to make the file.
What i have learned till now is use “list to csv text” for first stage list.
“List to csv table” for two stage list.
What about more stages list like here in your case and in my case

You might want to store the data in tables…
See also

Taifun

1 Like

You can save a list (single or containing sublists) in TinyDB, then load data.

See attached example. First time you run the app, global var MyList is saved to TinyDB as is.
Next time, press “Get data” to load your list, which is shown in lbShow label.

Hope this is within your needs.


LoadSaveLists.aia (1.4 MB)

2 Likes

ok
what i mean by storing is that making a file of the tinydb data. the requirement is you can share the file with other user and he can open the same data. this file may also be required to export the data to phone when you are changing the phone in case of offline apps.
in tinydb things are getting stored pretty well. i am able to do them. problem is i am not able to create a text file like csv file for more than 2 stage list which is required for sharing, exporting or importing the data


this is the global variable which i am storing as whole in tiny db but not able to make the file using “list to csv text” or “list to csv table”

I suppose you cannot use “list to csv text” or “list to csv table” for combined lists of more than 2 levels.
In this case you must have as many separators between your data as the number of levels.
In the attached example I used \n, “,” (comma) and “;” (semicolon) for 1st, 2nd and 3rd level respectively. Be careful: Do NOT include any of these chars in values of your data.

Button “Get data” gets data to file_receivers from TinyDB.
Button “Save data” stores data of file_receivers to your phone in the root directory.
Button “Load data” retrieves data to file_receivers from file.


LoadSaveLists2.aia (1.4 MB)

PS. I filled file_receivers with some data for better examination.
Enjoy

2 Likes

Thank you @nikif99
Great i was looking for such idea only

Awesome!!! I having troubles with API Response, because i come from APP Inventor, and there; Json are parse by Dictionaries and not List (of List)

I think :thinking: it works with list of lists and dictionary. … search for the subject​:+1:in community

1 Like

I search it, but i can’t parse json received, now i’ll try JsonUtils extension.