How to Extract a Column From a Multi-dimensional List in Python

A list is a data type that represents an ordered collection of objects. In Python, lists are written between square brackets [ and ] and contain comma-separated items. For example, the list [‘a’, ‘b’, ‘c’] contains three items, namely ‘a’, ‘b’ and ‘c’.

Python provides an easy way to loop through a list and get a specific column.

keyboards = [['q', 'w', 'r'],
            ['a', 's', 'd'],
            ['z', 'x', 'c']]
first_col = [row[0] for row in keyboards]
print(first_col) #output: ['q', 'a', 'z']

brands = [{'label': 'Samsung', 'model': '121312412'}, {'label': 'Apple', 'model': '789615'}]
labels = [brand['label'] for brand in brands]
print(labels ) #output: ['Samsung', 'Apple']

You can extract a column via for loop:

newlist = []

for i in oldlist:
    newlist.append(i['column_name'])

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close