Chambers
-- -- --

Stuck with a 'for' loop that removes items from the dictionary

Anonymous in /c/coding_help

0
With this dictionary: <br><br>```python<br>dct = {<br> 'Sonny Franco': 1.8, <br> 'Michael Harris': 6.4, <br> 'Luis Garcia': 4.8, <br> 'Fernando Tatis': 5.1, <br> 'Riley Greene': 3.3, <br> 'Mookie Betts': 6.1, <br> 'Sonny Gray': 5.2, <br> 'Spencer Strider': 3.8, <br> 'Rafael Devers': 4.1, <br> 'Kyle Manzardo': 1.2, <br> 'Mookie Betts': 4.5, <br> 'Chris Sale': 3.7, <br> 'Trea Turner': 5.8, <br> 'Ozzie Albies': 2.9, <br> 'Shane Bieber': 6.5, <br> 'Corbin Carroll': 4.1, <br> 'Ronald Acuña': 6.1, <br> 'Sonny Gray': 5, <br> 'Corbin Burnes': 3.3, <br> 'Emmanuel Clase': 3.2, <br> 'Brendan Donovan': 2.9, <br> 'Bo Naylor': 5.9, <br> 'Yordan Alvarez': 5.6, <br> 'Juan Yepez': 3.1, <br> 'Hunter Greene': 3, <br> 'Eric Pardinho': 1.6, <br> 'Bobby Witt': 6.6, <br> 'Luis Castillo': 4.9, <br> 'Matt Manning': 5.4, <br> 'Stetson Allie': 5.8, <br> 'Bryce Harper': 4.3<br>}<br>```<br><br>I am trying to create a `for` loop that will iterate through the dictionary and remove players with position numbers above 5. The position numbers vary between 1.2 to 6.6, so it will remove the players with position numbers 6.0 or higher. So far, I have come up with this code.<br><br>```python<br>for player, position in dct.items(): <br> if position >= 6.0:<br> del dct[player]<br>print(dct)<br>```<br><br>The issue I am facing is that it works, but not correctly. It doesn't remove all of the players with position numbers above 5. This could be an issue with the way dictionaries orders items, but I am not sure how to resolve this. Any help would be appreciated.

Comments (0) 4 👁️