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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:42:51+00:00 2026-06-12T20:42:51+00:00

In the below example I would expect all the elements to be tuples, why

  • 0

In the below example I would expect all the elements to be tuples, why is a tuple converted to a string when it only contains a single string?

>>> a = [('a'), ('b'), ('c', 'd')]
>>> a
['a', 'b', ('c', 'd')]
>>> 
>>> for elem in a:
...     print type(elem)
... 
<type 'str'>
<type 'str'>
<type 'tuple'>
  • 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-12T20:42:52+00:00Added an answer on June 12, 2026 at 8:42 pm

    why is a tuple converted to a string when it only contains a single string?

    a = [('a'), ('b'), ('c', 'd')]
    

    Because those first two elements aren’t tuples; they’re just strings. The parenthesis don’t automatically make them tuples. You have to add a comma after the string to indicate to python that it should be a tuple.

    >>> type( ('a') )
    <type 'str'>
    
    >>> type( ('a',) )
    <type 'tuple'>
    

    To fix your example code, add commas here:

    >>> a = [('a',), ('b',), ('c', 'd')]
    
                 ^       ^
    

    From the Python Docs:

    A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.

    If you truly hate the trailing comma syntax, a workaround is to pass a list to the tuple() function:

    x = tuple(['a'])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the Jquery example below, I would like to expand $(this) before it is
For this code example below, usually I would use [self fall]; instead of the
As the title suggest, I would like to do the above. Example below: library(stringr)
I'm looking for example of how I would solve the scenario below: Imagine my
How can I get only unique departments from the below example? Dept Id Created
I have a query similar to the example below (only column names changed for
The list signals_by_date stores tuples and each tuple contains 15 numbers. For each tuple
In the code below I would expect to see find_examples_out/.t1, find_examples_out/.t2 and find_examples_out/.s1 files
Let's consider the below example. There, I have: target MAIN calls target t and
Reproducible example below. I have a simulation loop, within which I occasionally have rows

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.