0

I have the following dataframe.

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]

from this list I would like to pick the second value from the list values and get the following list.

new_list = [4364, 791, 2847, 1296]

Can any one help on this?

3
  • Is the criteria always to get the second element of the tuple for each element in the list? Commented Jan 5, 2022 at 13:18
  • Yes. I would like to get the second element. Commented Jan 5, 2022 at 13:18
  • 2
    new_list = [i[1] for i in my_list]? Commented Jan 5, 2022 at 13:20

3 Answers 3

1

What you are describing are just lists not pandas DataFrames. In order to get the second element, the easiest solution is probably to use list comprehension:

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]
new_list = [x for _, x in my_list]
# or
new_list = [x[1] for x in my_list]

If you are indeed working with pandas, you can use [] to get desired column:

df = pandas.DataFrame([(343, 4364), (221, 791), (221, 2847), (21, 1296)])
col = df[1]
Sign up to request clarification or add additional context in comments.

Comments

1

use this snippets:

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]
res=[]
for i,x in my_list:
    res.append(x)

print(res)

Output:

[4364, 791, 2847, 1296]

Comments

1

You can do it with list comprehension:

my_list= [(343, 4364), (221, 791), (221, 2847), (21, 1296)]
new_list = [i[1] for i in my_list]
print(new_list)

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.