Photo by Didssph on Unsplash
In this article, I am converting the nested listed into a single list. We will convert the flattened list into a DataFrame. The structure of a nested list looks similar to this: [[list 1],[list 2],[list3], ..…, [list n]].
This is part of the data-preprocessing to generate the HTML map page shown below.
In the previous article, I scarped a website using BeautifulSoup, and the data is retrieved in the form of a nested list. In this article, I am converting the nested listed into a single list.
Import nested list from a text file
You can follow the steps provided in the previous article to generate your nested list or download the nested list from my Github repository. ‘sta.txt’ file contains a nested list of station names, and ‘add.txt’ contains a nested list of corresponding station addresses.
# 'sta.txt' contains nested list of stations
A new variable is created from the ‘sta’ to convert the nested list to a single list. The following code is used to flatten the nested list ‘sta’ to a list called ‘all_stations’.
# sta is a nested list [,,]
The same process is repeated to convert a nested list ‘add’ to a single list ‘all_address’. The code is given below:
# add is a nested list [,,]
Converting the lists to a DataFrame
To create a DataFrame, we will first assign the newly created list to pd.DataFrame and assign column name as ‘station’. We will also add a column that contains the station addresses. Both lines of codes are given below.
df = pd.DataFrame(all_stations,columns=['Stations'])df['Address'] = all_addressdf.head(10)
The next article will extract the information related to the latitude and longitudinal coordinates based on the addresses extracted from the web page and stored in the DataFrame. You can read the next article here: Part 3: Finding latitude and longitude of addresses using GoogleMaps API
A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list. You can use them to arrange data into hierarchical structures.
A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list.
You can use them to arrange data into hierarchical structures.
Create a Nested List
A nested list is created by placing a comma-separated sequence of sublists.
Access Nested List Items by Index
You can access individual items in a nested list using multiple indexes.
The indexes for the items in a nested list are illustrated as below:
Negative List Indexing In a Nested List
You can access a nested list by negative indexing as well.
count backward from the end of the list. So,
The negative indexes for the items in a nested list are illustrated as below:
Change Nested List Item Value
You can change the value of a specific item in a nested list by referring to its index number.
Add items to a Nested list
To add new values to the end of the nested list, use append() method.
When you want to insert an item at a specific position in a nested list, use insert() method.
You can merge one list into another by using extend() method.
Remove items from a Nested List
If you know the index of the item you want, you can use pop() method. It modifies the list and returns the removed item.
If you don’t need the removed value, use the del statement.
If you’re not sure where the item is in the list, use remove() method to delete it by value.
Find Nested List Length
You can use the built-in len() function to find how many items a nested sublist has.
Iterate through a Nested List
To iterate over the items of a nested list, use simple for loop.
How do I extract a column from a list in Python?
Use the syntax [row[i] for row in array] to extract the i - indexed column from array ..
an_array = [[1,2,3],.
column_one = [row for row in an_array].
How do you access a column in a list in Python?
Using the list() function. Pass the dataframe to the list() function to get the list of column names. print(list(df)) print(list(df)) ... .
Using df. columns. values. tolist() ... .
Using list comprehension. You can also get the columns as a list using list comprehension. print([col for col in df]) print([col for col in df]).
How do you sum a column in a nested list in Python?
We can find sum of each column of the given nested list using zip function of python enclosing it within list comprehension. Another approach is to use map(). We apply the sum function to each element in a column and find sum of each column accordingly.
How do you sum two nested lists in python?
Method 2: Add two list using the Comprehension List.
# initialize the Python lists..
lt1 = [2, 4, 6, 8, 10, 30].
lt2 = [2, 4, 6, 8, 10, 12].
# print the original list element..
print ( " Python list 1 : " + str (lt1)).
print ( "Python list 2 : " + str (lt2)).
# use list comprehension to add two lists..