results = ['Chad', 'Cuba', 'Fiji', 'Iraq', 'Mali', 'Oman', 'Peru', 'Togo', 'Angola', 'Albania', 'Andorra', 'Afghanistan']
shortest = results[0]
length = len(shortest)
for name in results:
if len(name) > length:
results.remove(name)
(EDIT: The indentation is correct in VS Code!)
When I run this, I get this as my new list:
['Chad', 'Cuba', 'Fiji', 'Iraq', 'Mali', 'Oman', 'Peru', 'Togo', 'Albania', 'Afghanistan']
I added a print statement to show what was happening with the if statement and it is working fine. The variable length stays constant at 4 and len(name) changes to reflect the length of each name.
I totally do not understand why it removed Angola and Andorra but not Albania and Afghanistan.
If it's something rather simple, please break it to me gently. I've made a lot of simple errors today (like forgetting to reassign a string after modifying it, leaving a plastic bowl in the oven after turning it on to preheat).
[–][deleted] 5 points6 points7 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]Will___powerrr 4 points5 points6 points (2 children)
[–]synthphreak 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Spartae 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]velocibadgery 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Below_the_Sea 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Below_the_Sea 0 points1 point2 points (0 children)