you can have a look (assume that your word is a string):
word = 'my_word'
print([(word[:i], word[i:]) for i in range(len(word) + 1)])
output:
[('', 'my_word'),
('m', 'y_word'),
('my', '_word'),
('my_', 'word'),
('my_w', 'ord'),
('my_wo', 'rd'),
('my_wor', 'd'),
('my_word', '')]
you can see that your last element has an empty string if you do not check for your R since you are using slicing(R[1:]), you will get: IndexError: string index out of range
, so yes it is necessary to check in this case
if R
is the way the checking is happing inside the list comprehension, the same logic as in a normal if statement.
you can have a look here to read more about if/else inside list comprehension