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.