How do you move an item to the front of a list python?

I have an array with a set of elements. I'd like to bring a given element to the front but otherwise leave the order unchanged. Do folks have suggestions as to the cleanest syntax for this?

This is the best I've been able to come up with, but it seems like bad form to have an N log N operation when an N operation could do.

    mylist = sorted(mylist,
                    key=lambda x: x == targetvalue,
                    reverse=True)

How do you move an item to the front of a list python?

Dharman

27.8k21 gold badges75 silver badges127 bronze badges

asked Jun 18, 2009 at 18:52

1

I would go with:

mylist.insert(0, mylist.pop(mylist.index(targetvalue)))

answered Jun 18, 2009 at 18:57

Mike HordeckiMike Hordecki

85.7k3 gold badges25 silver badges26 bronze badges

To bring (for example) the 6th element to the front, use:

mylist.insert(0, mylist.pop(5))

(python uses the standard 0 based indexing)

ZF007

3,6468 gold badges32 silver badges47 bronze badges

answered Jun 18, 2009 at 18:55

Alex MartelliAlex Martelli

823k163 gold badges1202 silver badges1379 bronze badges

1

This requires just two list operations (no index): mylist.remove(targetvalue) mylist.insert(0, targetvalue)

answered Feb 6, 2015 at 16:48

proskiproski

3,37326 silver badges25 bronze badges

Note: the following code (and the sample code you offered) will put all matching elements at the front.

x = targetvalue
for i in range(len(mylist)):
    if(mylist[i] == x):
        mylist = [mylist[i]] + mylist[:i] + mylist[i+1:]

For example, if mylist = [1, 2, 3, 4, 3] and x = 3, this will result in [3, 3, 1, 2, 4].

answered Jun 18, 2009 at 19:03

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The manipulation of lists is quite common in day-day programming. One can come across various issues where one wishes to perform using just one-liners. One such problem can be of moving a list element to the rear ( end of list ). Let’s discuss certain ways in which this can be done.

    Method #1 : Using append() + pop() + index()
    This particular functionality can be performed in one line by combining these functions. The append function adds the element removed by pop function using the index provided by index function.

    test_list = ['3', '5', '7', '9', '11']

    print ("The original list is : " + str(test_list))

    test_list.append(test_list.pop(test_list.index(5)))

    print ("The modified element moved list is : " + str(test_list))

    Output :

    The original list is : ['3', '5', '7', '9', '11']
    The modified element moved list is : ['3', '7', '9', '11', '5']
    

    Method #2 : Using sort() + key = (__eq__)
    The sort method can also be used to achieve this particular task in which we provide the key as equal to the string we wish to shift so that it is moved to the end.

    test_list = ['3', '5', '7', '9', '11']

    print ("The original list is : " + str(test_list))

    test_list.sort(key = '5'.__eq__)

    print ("The modified element moved list is : " + str(test_list))

    Output :

    The original list is : ['3', '5', '7', '9', '11']
    The modified element moved list is : ['3', '7', '9', '11', '5']
    


    How do you change the position of an element in a list Python?

    Method #1 : Using append() + pop() + index() This particular functionality can be performed in one line by combining these functions. The append function adds the element removed by pop function using the index provided by index function.

    How do I put a element in front of a list?

    Use the + Operator to Append an Element to the Front of a List in Python. Another approach to append an element to the front of a list is to use the + operator. Using the + operator on two or more lists combines them in the specified order.

    How do you move the last element of a list to the first?

    The last element is added to the rest of the list to achieve this task using slicing. This functionality can also be achieved using the inbuilt functions of python viz. insert() and pop() . The pop function returns the last element and that is inserted at front using the insert function.

    What does pop () do in Python?

    The pop() method removes the element at the specified position.