Python get column from nested list

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.

Final product

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 
content = open("sta.txt", "r")
sta = eval(content.read())
content.close()

# 'add.txt' contains nest list of corresponding station addresses
content = open("add.txt", "r")
add = eval(content.read())
content.close()

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 [[],[],[]]
all_stations = []
for stations in sta:
for station in stations:
all_stations.append(station)

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 [[],[],[]]
all_address = []
for addresses in add:
for address in addresses:
all_address.append(address)

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)

DataFrame

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

What is Python Nested List?

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.

L = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']

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:

L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

print(L[2])
# Prints ['cc', 'dd', ['eee', 'fff']]

print(L[2][2])
# Prints ['eee', 'fff']

print(L[2][2][0])
# Prints eee

Negative List Indexing In a Nested List

You can access a nested list by negative indexing as well.

Negative indexes count backward from the end of the list. So, L[-1] refers to the last item, L[-2] is the second-last, and so on.

The negative indexes for the items in a nested list are illustrated as below:

L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

print(L[-3])
# Prints ['cc', 'dd', ['eee', 'fff']]

print(L[-3][-1])
# Prints ['eee', 'fff']

print(L[-3][-1][-2])
# Prints eee

Change Nested List Item Value

You can change the value of a specific item in a nested list by referring to its index number.

L = ['a', ['bb', 'cc'], 'd']
L[1][1] = 0
print(L)
# Prints ['a', ['bb', 0], 'd']

Add items to a Nested list

To add new values to the end of the nested list, use append() method.

L = ['a', ['bb', 'cc'], 'd']
L[1].append('xx')
print(L)
# Prints ['a', ['bb', 'cc', 'xx'], 'd']

When you want to insert an item at a specific position in a nested list, use insert() method.

L = ['a', ['bb', 'cc'], 'd']
L[1].insert(0,'xx')
print(L)
# Prints ['a', ['xx', 'bb', 'cc'], 'd']

You can merge one list into another by using extend() method.

L = ['a', ['bb', 'cc'], 'd']
L[1].extend([1,2,3])
print(L)
# Prints ['a', ['bb', 'cc', 1, 2, 3], 'd']

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.

L = ['a', ['bb', 'cc', 'dd'], 'e']
x = L[1].pop(1)
print(L)
# Prints ['a', ['bb', 'dd'], 'e']

# removed item
print(x)
# Prints cc

If you don’t need the removed value, use the del statement.

L = ['a', ['bb', 'cc', 'dd'], 'e']
del L[1][1]
print(L)
# Prints ['a', ['bb', 'dd'], 'e']

If you’re not sure where the item is in the list, use remove() method to delete it by value.

L = ['a', ['bb', 'cc', 'dd'], 'e']
L[1].remove('cc')
print(L)
# Prints ['a', ['bb', 'dd'], 'e']

Find Nested List Length

You can use the built-in len() function to find how many items a nested sublist has.

L = ['a', ['bb', 'cc'], 'd']

print(len(L))
# Prints 3

print(len(L[1]))
# Prints 2

Iterate through a Nested List

To iterate over the items of a nested list, use simple for loop.

L = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]   
for list in L:
    for number in list:
        print(number, end=' ')
# Prints 1 2 3 4 5 6 7 8 9

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],.
[4,5,6],.
[7,8,9]].
column_one = [row[1] 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..