For a small test I am doing, I need to print the 5th element of a list, if there is one.
with open (my_file, 'r') as my_probe:
for fileLine in my_probe:
src_string = fileLine.split()
my_list = list(src_string) # The list may contain any number of items
if len(my_list) > 3: # Only if lists is longer than 4 items
print("Info I need: "+my_list[4])
When I run the code, however, it nevertheless tries to print the 5th element of lists with less then 5 elements and thus returns an error showing I am trying to print unexisting item:
Traceback (most recent call last):
File "my_script.py", line 70, in <module>
print ("Info I need: "+my_list[4])
IndexError: list index out of range
Could anyone suggests a way to overcome this problem?
Thanks in advance!