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

  • SEARCH
  • Home
  • 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 8910991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:49:55+00:00 2026-06-15T03:49:55+00:00

I have a list which contains first and last names, like this one: names

  • 0

I have a list which contains first and last names, like this one:

names = ["John Smith", "Rob Julian", "Eric Walls"]

I want to get only the first names in this list.

I achieved that by doing:

first_names = [n.split(" ")[0] for n in names]

And that gave me the wanted result.

But in my opinion this is pretty ugly, is there any better way to achieve that goal?

  • 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-15T03:49:57+00:00Added an answer on June 15, 2026 at 3:49 am

    Yes, but not really. Performance wise you are better off with what you have.

    first_names = []
    for n in names:
        first_names.append(n.split()[0])
    

    would work, but i love list comprehension in python. I mean whats wrong with

    first_names = [n.split()[0] for n in names]
    

    For fun, you could also do the following. I would imagine if you are processing a very large list this might have the best performance. But, you might want to investigate that first.

    first = lambda n : n.split()[0]
    first_names = [first(name) for name in names]
    

    to make this comprehensive, you could also use the lambda to map it.

    first = lambda n : n.split()[0]
    first_names = map(first,names)
    

    Per comments, I am adding yet another way

    from operator import itemgetter
    first_names = map(itemgetter(0), map(str.split, names))
    

    in conclusion, yes there are other ways to do it.

    but your original seems to be the most preferred. If speed is an issue you may want to tinker with the others.

    updated with time

    not the most scientific, but using a list of approximaltey 3.5million names, I ran the above calling the files n0-4 and ran time n0;time n1; time n2; time n3; time n4
    here are my results. Looks as if the original list comprehension was the fastest on my machine.

    real    0m8.433s
    user    0m7.064s
    sys     0m1.288s
    
    real    0m8.213s
    user    0m6.852s
    sys     0m1.300s
    
    real    0m8.581s
    user    0m7.240s
    sys     0m1.264s
    
    real    0m8.374s
    user    0m7.164s
    sys     0m1.140s
    
    real    0m11.890s
    user    0m10.101s
    sys     0m1.672s
    

    (I ran them a few times in differing orders, times were consistent.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list which contains a dictionary of ExpandoObjects. Im binding this to
I have this to-many relationship which contains at least one element: Appointment <<------>> Invitee
I have a List which contains a list of objects and I want to
I have a list which contains some items of type string. List<string> lstOriginal; I
I have a list which contains dictionaries. Each dictionary has an attribute description which
I have a python list which contains dictionaries and I want to make a
I have a JTree which contains file/directory, i want to get a list which
I have a list a simple dialog box which contains a few checkboxes, I
I have an array list named newSymptomList which contains a list of Symptom id's
I have an List<int> which contains 1,2,4,7,9 for example. I have a range from

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.