Say I have a word list of thousands of words. But, they're all the same length. so say for example, all 5 letter words, and I want to find all the words that have 'o' as the middle letter (3rd position, 2nd in the index since it starts from 0).
How do I go about removing all the other words in the original list so we can prune it down from there?
I'll be doing this multiple times, up to 4 times in this case, since the words would all be 5 letters exactly.
So it's like original_list = ["house", "ghost", "there", "loose"] -- stuff like that. And my output should be original_list = ["ghost", "loose"]
Ideally instead of appending to a new list, I'd rather just remove items that don't match from the original list. That way I can keep pruning it down from there in each iteration.
CodePudding user response:
The most sensible way to do this is to create a new list and assign it to your original list variable.
original_list = ["house", "ghost", "there", "loose"]
original_list = [e for e in original_list if e[2] == 'o']
CodePudding user response:
You just index it from the original list First index is 0 "house" First index is "ghost" etc.
original_list = ["house", "ghost", "there", "loose"]
print(original_list[1])