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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:15:17+00:00 2026-05-27T03:15:17+00:00

>>> c = ‘A/B,C/D,E/F’ >>> [a for b in c.split(‘,’) for (a,_) in b.split(‘/’)]

  • 0
>>> c = 'A/B,C/D,E/F'
>>> [a for b in c.split(',') for (a,_) in b.split('/')]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
ValueError: need more than 1 value to unpack

The expected result is ['A', 'C', 'E'].

This is how I’d expect to do it, but apparently it’s back to front in Python:

>>> [a for (a, _) in b.split('/') for b in c.split(',')]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
  • 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-05-27T03:15:18+00:00Added an answer on May 27, 2026 at 3:15 am

    The reason you are failing is b.split('/') is not yielding a 2-tuple. A double list comprehension implies you want to treat the cartesian product as a flat stream and not a matrix. That is:

    >>> [x+'/'+y for y in 'ab' for x in '012']
    ['0/a', '1/a', '2/a', '0/b', '1/b', '2/b']
        # desire output 0,1,2
        # not output 0,1,2,0,1,2
    

    You are not looking for 6 answers, you are looking for 3. What you want is:

    >>> [frac.split('/')[0] for frac in c.split(',')]
    ['A', 'C', 'E']
    

    Even if you used a nested list comprehension, you would get you the cartesian product (3×2=6) and realize that you have duplicate information (you don’t need the x2):

    >>> [[x+'/'+y for y in 'ab'] for x in '012']
    [['0/a', '0/b'], ['1/a', '1/b'], ['2/a', '2/b']]
        # desire output 0,1,2
        # not [0,0],[1,1],[2,2]
    

    The following are equivalent ways to do things. I sort of gloss over the major difference between generators and lists in this comparison though.

    Cartesian product in list form:

    ((a,b,c) for a in A for b in B for c in C)
                #SAME AS#
    ((a,b,c) for (a,b,c) in itertools.product(A,B,C))
                #SAME AS#
    for a in A:
        for b in B:
            for c in C:
                yield (a,b,c)
    

    Cartesian product in matrix form:

    [[[(a,b,c) for a in A] for b in B] for c in C]
                #SAME AS#
    def fC(c):
        def fB(b,c):
            def fA(a,b,c):
                return (a,b,c)   
            yield [f(a,b,c) for a in A]
        yield [fB(b,c) for b in B]
    [fC(c) for c in C]
                #SAME AS#
    Cs = []
    for c in C:
        Bs = []
        for b in B:
            As = []
            for a in A:
                As += [a]
            Bs += [As]
        Cs += [Bs]
    return Cs
    

    Repeated application of function to list

    ({'z':z} for x in ({'y':y} for y in ({'x':x} for x in 'abc')))
                  #SAME AS#
    for x in 'abc':
        x2 = {'x':x}
        y2 = {'y':x2}
        z2 = {'z':y2}
        yield z2
                  #SAME AS#
    def f(x):
        return {'z':{'y':{'x':x}}}
    return [f(x) for x in 'abc']     # or map(f,'abc')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
The problem with unsigned char. I am reading a PPM image file which has
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back 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.