-1

I just started coding and I am stuck here. Suppose I have a list:

arr=[["ashley",25,399.9],["tracey",26,990.45],["jimmy",23,987],["nancy",20,1000.1]]

I want all integer value in another list

age=[]
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    ["pythonic"](https://stackoverflow.com/questions/25011078/what-does-pythonic-mean#:~:text=Pythonic%20means%20code%20that%20doesn,is%20intended%20to%20be%20used.) is the correct term. – cs95 Jul 08 '20 at 06:58
  • 5
    Please show what you’ve tried so far and where you’ve gotten stuck. What have you researched on this topic? – S3DEV Jul 08 '20 at 06:58
  • See this answer: https://stackoverflow.com/a/4357842/2570277 – Nick Jul 08 '20 at 07:06
  • Do you need to detect if the values are integers? Or are you just going to take the value in that position from each sub-list? Or what exactly is the rule that tells you what should go into `age`? – Karl Knechtel Jul 08 '20 at 07:11

2 Answers2

3

You can use List Comprehension

arr = [["ashley",25,399.9],["tracey",26,990.45],["jimmy",23,987],["nancy",20,1000.1]]

age = [v for i in arr for v in i if str(v).isnumeric()]

Out: [25, 26, 23, 987, 20]

This one is from @Leo Arad makes slightly faster:

age = [v for i in arr for v in i if isinstance(v, int)]

Out: [25, 26, 23, 987, 20]
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85
0

yes you make

arr=[["ashley",25,399.9],["tracey",26,990.45],["jimmy",23,987],["nancy",20,1000.1]]
age=[i[1] for i in arr]
Yash Goyal
  • 174
  • 1
  • 8