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 7855505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:24:26+00:00 2026-06-02T20:24:26+00:00

I am trying to find out if there is a way to split the

  • 0

I am trying to find out if there is a way to split the value of each iteration of a list comprehension only once but use it twice in the output.

As an example of the problem I am trying to solve is, I have the string:

a = "1;2;4\n3;4;5"

And I would like to perform this:

>>> [(x.split(";")[1],x.split(";")[2]) for x in a.split("\n") if x.split(",")[1] != 5]
[('2', '4'), ('4', '5')]

Without the need for running split three times. So something like this (Which is obviously invalid syntax but hopefully is enough to get the message across):

[(x[1],x[2]) for x.split(";") in a.split("\n") if x[1] != 5]

In this question I am not looking for fancy ways to get the 2nd and 3rd column of the string. It is just a way of providing a concrete example. I could for course for the example use:

[x.split(";")[1:3] for x in a.split("\n")]

The possible solutions I have thought of:

  1. Not use a list comprehension
  2. Leave it as is
  3. Use the csv.DictReader, name my columns and something like StringIO to give it the input.

This is mostly something that would be a nice pattern to be able to use rather than a specific case so its hard to answer the “why do you want to do this” or “what is this for” kind of questions

Update: After being reading the solution below I went and ran some speed tests. And I found in my very basic tests that the solution provided was 35% faster than the naive solution above.

  • 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-02T20:24:27+00:00Added an answer on June 2, 2026 at 8:24 pm

    You could use a list comprehension wrapped around a generator expression:

    [(x[1],x[2]) for x in (x.split(";") for x in a.split("\n")) if x[1] != 5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm trying to find out if there is a way to set the
I am trying to find out if there is a more optimal way for
I am trying to find out if there is a way to detect the
I'm currently trying to find out if there is a way to allow our
I'm trying to find out if there is any way to auth into some
I'm trying to find out if there is a way to determine if a
I'm trying to find out if there is any way to build my flex
I'm trying to find out if there is any way to find specific element
I'm trying to find out if there's a cleaner way to write something like
I am trying to find out if there is a better way to do

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.