-2

Not sure how to google this for the same reason I am not sure how to write the title.

basically if I have the array:

[[1,2,3],[4,5,6],[7,8,9]]

I want to pull say the 2nd(nth) item of each array and turn them into an array as such:

[2,5,8]

What is the quickest (preferably immediate) way to parse the array this way without using for-loops?

Libra
  • 2,544
  • 1
  • 8
  • 24

2 Answers2

3
  • You can use list comprehension
x = [[1,2,3],[4,5,6],[7,8,9]]
y = [ele[1] for ele in x]
  • If you really don't want see loop or for, you can use map and lambda
x = [[1,2,3],[4,5,6],[7,8,9]]
y = list(map(lambda x:x[1],x))
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21
0

If you don't consider a list comprehension a loop (even though it is certainly looping under the hood), you could accomplish this like:

list1 = [[1,2,3], [4,5,6], [7,8,9]]
list2 = [ e[1] for e in list1 ]