Created
October 4, 2016 12:56
-
-
Save thomasvnl/41cf490e2532affa48d6e17ee319c1e5 to your computer and use it in GitHub Desktop.
Promote an item in a list to the first position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def promote_item_to_first_position(enumerator, key, value): | |
""" | |
Promotes an item to the first index of the list if it isn't already there. It moves the item from it's current | |
position and resets the list's indexes. Be careful to not use this on a giant list, because the operation could | |
be quite heavy. With small lists the impact is not noticeable. | |
:param enumerator: | |
:param key: | |
:param value: | |
:return: | |
""" | |
for idx, item in enumerate(enumerator): | |
if item[key] == value: | |
if idx != 0: | |
del enumerator[idx] | |
enumerator.insert(0, item) | |
break | |
return enumerator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment