0

Python newbie here! I have a list like this:

list_a = [[a,b,c], [d,e,f]]

And I'm trying to find a way to look at only the nth value in each list within the larger list.

i.e. So both a and d where want the first value in each

I understand I can do this:

  • a -> list_a[0][0]
  • d -> list_a[1][0]

but is there a way I can pull both together to form their own list?

Thanks.

01ja
  • 11
  • 2

2 Answers2

3

Use list comprehension:

list_a = [['a','b','c'], ['d','e','f']]
n = 1
list_b = [lst[n] for lst in list_a]
print(list_b)
# ['b', 'e']
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • 3
    I don't think a self-proclaimed "Python newbie" will understand the use of a list comprehension without further explanation – crock Jun 16 '22 at 19:06
  • this is the wrong result: ['b', 'e']. n should be 0 not 1 – JacobIRR Jun 16 '22 at 19:06
  • 1
    @JacobIRR that is the correct result, both those characters are at index `1` in their respective subarrays. You can change `n` to whatever index value you desire, including `0`. – crock Jun 16 '22 at 19:07
  • 1
    @crock that is because instructors teach Python wrongly. The code using a list comprehension is easier to understand than the equivalent for loop, as it directly describes the desired result rather than focusing on the mechanics of creating it. – Karl Knechtel Jun 16 '22 at 19:15
  • Fair enough, but in this case we have no idea if the OP even knows how to use a for loop. This could be a good opportunity to learn the mechanics of a for loop. I would also argue learning the mechanics of a for loop are much more important than learning how to use a list comprehension. It seems to me like learning short division before long division. Sure, both are perfectly usable, but long division teaches a concept in broken-down steps which might be easier for a newcomer to understand. – crock Jun 16 '22 at 19:27
0

I would suggest you consider this problem from matrix perspective. That is for your list_a = [['a','b','c'],['d','e',f']], see it as a matrix with two rows and three columns.

Then, what you described as 'nth' value in all sub list is essentially nth columns in this matrix.

Like this

    1   2   3
1   a   b   c
2   d   e   f

Now since you are using 1-D list as the datas structure to represent a 2D matrix, you can only access easily from one direction, regardless you view it as row, or column. That is, a sub list can only represent 'row-direction' or a 'column-direction'

So the idea is to switch the row and column. We call this operation Transpose.(https://en.wikipedia.org/wiki/Transpose)

Row becomes columns, and columns become rows. As the example above, after transposing, the matrix should become:

    1   2
1   a   d 
2   b   e
3   c   f

The corresponding list will become [['a','d'],['b','e'],['c',f']]

Now you can easily access the 'nth columns' (now is nth row) with index, since they are become rows and is a sub-list now.

There are multiple ways to do this.

  1. Using zip() You can use python's builtin function zip() to do the transpose.

     list_a = [['a','b','c'],['d','e','f']]
     t_list_a = list(zip(*list_a))
     t_list_a
     [('a', 'd'), ('b', 'e'), ('c', 'f')]
    

Do note that you need to pay attention to the shape of your matrix. That is if all rows (or columns) are in same size or not. Special exception handling might be required.

  1. List comprehension and traditional for loop.

This approach has already been mentioned by other good answers. I will skip it here.

  1. numpy

numpy as a popular library provides very powerful functions to help you with matrix related operation.

    import numpy
    list_a = [['a','b','c'],['d','e','f']]
    t_list_a = numpy.transpose(list_a)
    t_list_a
    array([['a', 'd'],
           ['b', 'e'],
           ['c', 'f']])
Arthur Wang
  • 3,118
  • 4
  • 21
  • 29