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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:12:50+00:00 2026-06-04T19:12:50+00:00

Suppose I have the list, which I have edited so it’s not just called

  • 0

Suppose I have the list, which I have edited so it’s not just called “list” anymore

my_list = ['a b', 'b c d e', 'c', 'd e f g h', 'e f g h i j', 'f g h', 'g h']

I am trying to examine a specific element in the list, and see if one of the elements contains a certain string. I’ve been using something along the lines of the code below:

for i in range(len(my_list)):
    splitList = my_list[i].split(' ')
    if splitList[3] == "c":
        print "True"
    else:
        print "False"

but what I really want to do is check to see if splitList[3] even exists, and if it does, if it == “c” or simply print the 3rd “thing” in the element. (I’m being generic in my question, but my actual data is looking for a specific 3 character string) I’m certain regular expressions would solve all my problems but I’ve been looking for the perfect regex solution for days and am overwhelmed and without a solution. My data is very predictable and and I just need to check if the second word in an element of a list is there or not.

Is there a simple pythonic way to check to see if the a list even HAS something at a specific index, and if it does to go on from there?


If you want to suggest a regex solution,
the 24th element in my list is always
“G# Abc” ‘#’ can be 1-12 inclusive
and then the 25th element may also be
” G# Abc” with ‘#’ as 1-12
if the 25th element is not in the format ” G# Abc”, then the element and any other elements is not relevant. If it is in the format ” G# Abc”, I need to add the number to a new list.

  • 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-04T19:12:51+00:00Added an answer on June 4, 2026 at 7:12 pm

    Given an arbitrary string 'x y z', split into a list ['x', 'y', 'z'] with 'x y z'.split(), the simple pythonic way to test whether an index exists is len. Modifying your code above slightly:

    # don't use `list` as a variable name; it masks the built-in `list` constructor
    str_list = ['a b', 'b c d e', 'c', 'd e f g h', 'e f g h i j', 'f g h', 'g h']
    for s in str_list:
        split_list = s.split(' ')
        if len(split_list) > 3 and split_list[3] == 'c':
            print "True"
        else:
            print "False"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

example, suppose I have one list which contains 45 element so from which i
Suppose I have a list, called elements , each of which does or does
Suppose I have a list, in which no new nodes are added or deleted.
Suppose I have a list that I wish not to return but to yield
Suppose I have a list of lists of elements which are all the same
Suppose I have a list in sml which is very big then sml shows
Suppose I have a List<IMyInterface> ... I have three classes which implement IMyInterface :
Suppose I have a list of nodes which represent a nested set hierachy (examples
Lets suppose I have a RESTful service which has a list of tasks. GET
I have been given a staff list which is supposed to be up to

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.