1

Hi does anyone know if I can filter a nested list to return only the second value in each sub-list? I can do it using a for-loop, just wondering if it possible to use the filter method?

weather_data = [['1', 'sunny'], ['2', 'rainy'], ['3', 'sunny']]

return:

weather = ['sunny', 'rainy', 'sunny']
0

2 Answers 2

1

How about to use a list comprehension:

values = [x[1] for x in weather_data]
print (values)
Sign up to request clarification or add additional context in comments.

Comments

1

Supposing the snake_case you have chosen in your question implies Python, try map function:

weather_data = [['1', 'sunny'], ['2', 'rainy'], ['3', 'sunny']]
result = list(map(lambda x: x[1], weather_data))
print(result)

If not Python, plenty of other languages have this functionality in slightly different syntaxes:

https://en.wikipedia.org/wiki/Map_(higher-order_function)#Language_comparison

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.