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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:26:45+00:00 2026-06-16T03:26:45+00:00

I’m working on a little set of scripts in python, and I came to

  • 0

I’m working on a little set of scripts in python, and I came to this:

line = "a b c d e f g"
a, b, c, d, e, f, g = line.split()

I’m quite aware of the fact that these are decisions taken during implementation, but shouldn’t (or does) python offer something like:

_, _, var_needed, _, _, another_var_needed, _ = line.split()

as well as Prolog does offer, in order to exclude the famous singleton variables.

I’m not sure, but wouldn’t it avoid unnecessary allocation? Or creating references to the result of the split call does not count up as overhead?

EDIT:

Sorry, my point here is: in Prolog, as far as I’m concerned, in an expression like:

test(L, N) :-
    test(L, 0, N).
test([], N, N).
test([_|T], M, N) :-
    V is M + 1,
    test(T, V, N).

The variable represented by _ is not accessible, for what I suppose the reference to the value that does exist in the list [_|T] is not even created.

But, in Python, if I use _, I can use the last value assigned to _, and also, I do suppose the assignment occurs for each of the variables _ — which may be considered an overhead.

My question here is if shouldn’t there be (or if there is) a syntax to avoid such unnecessary attributions.

  • 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-16T03:26:46+00:00Added an answer on June 16, 2026 at 3:26 am

    _ is a perfectly valid variable name and yes, you can use a variable multiple times in an unpacking operation, so what you’ve written will work. _ will end up with the last value assigned in the line. Some Python programmers do use it this way.

    _ is used for special purposes by some Python interactive shells, which may confuse some readers, and so some programmers do not use it for this reason.

    There’s no way to avoid the allocation with str.split(): it always splits the whole line, and the resulting strings are always allocated. It’s just that, in this case, some of them don’t live very long. But then again, who does?

    You can avoid some allocations with, say, re.finditer():

    import re
    
    fi = re.finditer(r"\S+", line)
    next(fi)
    next(fi)
    var_needed = next(fi).group()
    next(fi)
    next(fi)
    another_var_needed = next(fi).group()
    # we don't care about the last match so we don't ask for it
    

    But next() returns a Match object and so it’ll be allocated (and immediately discarded since we’re not saving it anywhere). So you really only save the final allocation. If your strings are long, the fact that you’re getting a Match object and not a string could save some memory and even time, I guess; I think the matched string is not sliced out of the source string until you ask for it. You could profile it to be sure.

    You could even generalize the above into a function that returns only the desired tokens from a string:

    import re
    
    def get_tokens(text, *toknums):
        toknums = set(toknums)
        maxtok = max(toknums)
        for i, m in enumerate(re.finditer(r"\S", text)):
            if i in toknums:
                yield m.group()
            elif i > maxtok:
                break
    
    var1, var2 = get_tokens("a b c d e f g", 2, 5)
    

    But it still ain’t exactly pretty.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.