How to convert a list of tuples to a matrix for column access?

Viewed 1514

I have a list of tuples:

tups = [('a',5,'test'),('b', 8, 'test1')]

For each tuple, I need to extract the value at an index i.

Obviously, I could iterate over each tuple in the list, get the value I am after and pop it into an array... However, I think there should be a nicer way.

Ideally, I'd like to transform my list of tuples to a matrix or DataFrame (or something like that), where I can extract a column as an array.

For example, using the above list of tuples, I would like to do something like the following:

matrix = tups.AsMatrix(ncols=len(tups[0])) #All tups have the same number of values.
column_values = matrix[0]
print(column_values)
> 'a'
> 'b'

How can I do this with Python 3? I have seen Pandas has a DataFrame type, but ideally I would like to do this without using another library (other than numpy or native python).

3 Answers
Related