How do I move an element in my list to the end in python
list, python
Solution
>>> lst = ['string1', 'string2', 'string3']
>>> lst.append(lst.pop(lst.index('string2')))
>>> lst
['string1', 'string3', 'string2']
We look for the index of `'string2'`, pop that index out of the list and then append it to the list.
Perhaps a somewhat more exception free way is to add the thing you're looking for to the end of the list first (after all, you already presumably know what it is). Then delete the first instance of that string from the list:
>>> lst = ['string1', 'string2', 'string3']
>>> lst.append('string2')
>>> del lst[lst.index('string2')] # Equivalent to lst.remove('string2')
>>> lst
['string1', 'string3', 'string2']
Problem
I have a list of strings called values and I want to make an element in the list to be the very last element. For example, if I have the string: ``` ['string1', 'string2', 'string3'] ``` I want string2 to be the very last element: ``` ['string1', 'string3', 'string2'] ``` There also may be an instance when my list does not contain string2. Is there an easy way to do this? This is what I have so far: ``` if 'string2' in values: for i in values: #remove string2 and append to end ```