Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:36:14+00:00 2026-06-14T09:36:14+00:00

Possible Duplicate: First items in inner list efficiently as possible Lets say I have:

  • 0

Possible Duplicate:
First items in inner list efficiently as possible

Lets say I have:

a = [ [1,2], [2,9], [3,7] ]

I want to retrieve the first element of each of the inner lists:

b = [1,2,3]

Without having to do this (my current hack):

for inner in a:
    b.append(inner[0])

I’m sure there’s a one liner for it but I don’t really know what i’m looking for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:36:15+00:00Added an answer on June 14, 2026 at 9:36 am

    Simply change your list comp to be:

    b = [el[0] for el in a]
    

    Or:

    from operator import itemgetter
    b = map(itemgetter(0), a)
    

    Or, if you’re dealing with “proper arrays”:

    import numpy as np
    a = [ [1,2], [2,9], [3,7] ]
    na = np.array(a)
    print na[:,0]
    # array([1, 2, 3])
    

    And zip:

    print zip(*a)[0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: iterate through pairs of items in python list I have a list
Possible Duplicate: Capitalize First Char of Each Word in a String Java I have
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Is there a native jQuery function to switch elements? I have list
Possible Duplicate: Get first element of an array What is the fastest and easiest
I have a classic remove duplicate items from list/nestedlist problem. However, due to the
Possible Duplicate: Finding first and last index of some value in a list in
I have two lists. I need to determine which word from the first list
Possible Duplicate: Open dropdown(in a datagrid view) items on a single click We have
Possible Duplicate: Replace only first match using preg_replace I have a string as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.