Skip to content

Instantly share code, notes, and snippets.

@thomasvnl
Created October 4, 2016 12:56

Revisions

  1. thomasvnl created this gist Oct 4, 2016.
    18 changes: 18 additions & 0 deletions promote_item_to_first_position.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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