Skip to content

Instantly share code, notes, and snippets.

@thomasvnl
Created October 4, 2016 12:56
Show Gist options
  • Save thomasvnl/41cf490e2532affa48d6e17ee319c1e5 to your computer and use it in GitHub Desktop.
Save thomasvnl/41cf490e2532affa48d6e17ee319c1e5 to your computer and use it in GitHub Desktop.
Promote an item in a list to the first position
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