I have a for loop that iterates through a list. However, at the end of the loop, the list goes out of range.
for i in range(0, 4):
x = case1[row + i][col:]
y = longest_chain(x)
one_counter.append(y)
print(case1[row + i -1][col:]
when checking row + i - 1, list index runs out of range. How can i ensure it doesn't by stopping when the list ends?
I don't know exactly what you want as an output, but based on your question here is how you would access the first and third element of every string, without a nested list:
cards = ['King of Hearts', '4 of Clubs', '8 of Clubs', 'Queen of Clubs', '9 of Diamonds']
for card in cards:
string_list = card.split(' ')
rank = string_list[0] # 1st word
suit = string_list[2] # 3rd word
print(f'rank: {rank} | suit:{suit}')
print('----------')
Output:
rank: King | suit:Hearts
----------
rank: 4 | suit:Clubs
----------
rank: 8 | suit:Clubs
----------
rank: Queen | suit:Clubs
----------
rank: 9 | suit:Diamonds
----------