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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:58:29+00:00 2026-05-27T04:58:29+00:00

Maybe there was a better way to do this, but I have a list

  • 0

Maybe there was a better way to do this, but I have a list of mixed elements:

outlist = [10, [ETSc, Juniper Hairstreak], [ETSc, Spotted Turtle],
           [ETSc, Blanding's Turtle], IWWH]

What I would like to do is iterate over this list and if the list element contains ETSc, then I want to convert it to a string like this:

ETSc (Juniper Hairstreak)

If it doesn’t contain ETSc then I just want to convert it to a string:

IWWH

Ultimately the strings will populate a variable in an access database table. But I have no idea how to tell Python to find the list elements containing ETSc, since some of the elements are lists within a list and some are just strings.

Any help is greatly appreciated!!

  • 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-27T04:58:30+00:00Added an answer on May 27, 2026 at 4:58 am

    Something like this, using a list comprehension:

    >>> input = [10, ["ETSc", "Juniper Hairstreak"], ["ETSc", "Spotted Turtle"], ["ETSc", "Blanding's Turtle"], "IWWH"]
    >>> output = [elt[0] + " (" + elt[1] + ")" if type(elt) == list and elt[0] == "ETSc" else str(elt) for elt in input]
    >>> output
    ['10', 'ETSc (Juniper Hairstreak)', 'ETSc (Spotted Turtle)', "ETSc (Blanding's Turtle)", 'IWWH']
    

    As @julio commented, you could make this more readable using a function:

    def xform(elt):
        if type(elt) == list and len(elt) > 1 and elt[0] == "ETSc":
            return elt[0] + " (" + elt[1] + ")"
        else:
            return str(elt)
    
    output = [xform(elt) for elt in input]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe there is no difference, but is either way better than the other (or
Maybe there's no way to solve this the way I'd like it but I
Is there a better way of writing the following method? Using LINQ maybe? Instead
Maybe there is a proper term for this, but what if you want to
I'm not very good at regex but maybe there's a simple way to achieve
I call this a flash meeting, but maybe there is another more appropriate name.
There may be a better way of doing this and if there is please
I have a list of update querys, but was wondering if there's a quicker
Surely there is a better way to do this? results = [] if not
There's probably a better way to do this than what I'd doing, because I'm

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.