Nested List
top of page
  • Writer's picturetechhead404

Nested List

What Are Nested List.

Almost all programs use some type of data. That data comes and many shapes and sizes, but is almost always storied in a list or array. Since I will be using python in the article I will use list. List is a group of data storied inside [] brackets. Using commas to separate the objects.



Why And When Do You Use Them.

List are the most common way to a API to return data. So chances are you have or will come across

them. Storing files in a list makes it easy to modify or retrieve data. Think of taking in a inventory document and being able to search that file for prices and items to store in your program for later

processing. Getting charting information from a API and only needing the opening price of a stock. In both cases you will have to work through the data to get only the data you need. Lets take a look at how we could get only the parts we want.


How To Break Them Down And Get The Data You Want.

Ok so we have are data pulled in, but it has 500 nested list each list have 6 different objects.

All we need is the open price of each nested list. This might seem like a mind bender and all the data that prints out might seem crazy at first. Trust me when I say once you know how to work through a list you will see how simple it really is.


Here you can see we make a call to fetch_ohlcv which returns a nested list of data from the API. The first thing we need to do is store that in a variable which I named data_list. After we have the data we need to create a empty list for the data we need. That is the open_price = [] that you see.

Now with the empty list made we can use a for loop to work through each nested list. The main list is named data_list and the item is used as place holder text for each list in data_list.

With the for loop ready we need to tell the program what to do with each item in the main list. We will do a simple .append here. The .append will add each item to the end of the empty list we created. Remember that list are Zero Indexed so if you want the first object you could use index 0. Since we want the second object from the list we will use (item[1]).

Now the program will pull the data, loop through it and add only the data we need to the empty list. Using a simple print statement we can check the results.

And there you have it. All the open prices from the API call. Working with list in Python can seem tough at first but after you get some practice even the biggest API data sets wont be a problem.

As you work with list more you will find even other ways to break down the data. I wanted to show this way so you have a simple understanding of the concepts used. Be sure to check out my other post and subscribe so you don't miss future post.

148 views0 comments

Recent Posts

See All
Post: Blog2 Post
bottom of page