Change position of element in list python

I have a list that is made of class instances:

MyList = [, , ]

I would like to change the position of the third element, , that should now stay in the position index = 1, so with the following output:

MyList = [, , ]

I have built a simple example that works:

a = [1,2]
b = [3,4]
c = [5,6]
d = [a,b,c]

The code above gives me the following print d output:

d = [[1, 2], [3, 4], [5, 6]]

I'm able to swap the elements (1) and (2) with the following method:

d.remove(c)
d.insert(c,1)

which gives me the following output (that is the one that I want):

d = [[1, 2], [5, 6], [3, 4]]

However, when I try to make the same with my list of instances, I get the following AttributeError:

AttributeError: entExCar instance has no attribute '__trunc__'

Someoene can tell me if I'm wrong in the method (example: you cannot use this technique with list of instances, you should rather do "this or that") or in the way I'm setting the code? The following script is the actual code I'm trying to run getting the error:

newElement = self.matriceCaracteristiques[kk1]    
self.matriceCaracteristiques.remove(newElement) 
self.matriceCaracteristiques.insert(newElement,nbConditionSortieLong)   

Thanks in advance.

EDIT: SOME DETAILS MORE

entExCar is the class which is being instatiating self.matriceCaracteristiques is the list I want to manipulate newElement is the element I want to remove from its original position (kk1) and put back into the new position (nbConditionSortieLong).

Suppose I've a list and now I want to change the position of one of it's elements. For instance, I want to change the position of A[i] to j. How can I do it efficiently (with less runtime and memory)? #if you are non-pythonistas then please share the algorithm **not swapping ex. [1,2,3,4,5] > [1,4,2,3,5] #moving 4 from position 3 to 1 **its INSERTION **I've got one: A.insert(j, A.pop(i)) #It is not an efficient approach, because pop is deleting an element, and reducing the size of the list, and again insert is adding an element, and increasing the size of list. So, the length of the array is changing for 2 times, and change of length is expensive for any type of array. [Updated on March 5, 2021] Hello everyone! For the last few days I was trying to solve the above problem in the fastest way possible. Here goes my implementation: https://code.sololearn.com/cr6MgZwo82P1/?ref=app . If anyone has got some other idea to solve the same, then please let us know. Thanks!

Till now this is the best approach: def insert_n_to_i(A, i, n): """slice the list and change the order to bring the target to the new position""" if i < n: A[n], A[i:n] = A[i], A[i+1:n+1] else: A[n], A[n+1:i+1] = A[i], A[n:i] If you know something better, then please reply here. Thanks!

Change position of element in list python

Faheem Hossain , why are you thinking your code is non-efficient? Did you make some tries with measuring time complexity and memory consumption? And if yes, please show us the code you compared it with, and also the result of your measurement. Please also tell us on which system you ran that code like sololearn playground or local IDE. Thanks!

Change position of element in list python

I guess you don't want swapping like this? mylist = [1, 2, 3] mylist[0], mylist[1] = mylist[1], mylist[0] print(mylist) # [2, 1, 3] https://code.sololearn.com/cNug95I9rG1u

Change position of element in list python

Faheem Hossain , the code you provided does just a simple swapping. When you are looking for a way to do something as you mentiond in your last comment, you should try it by yourself first. Please present your code here that we can help you. Please provide us with a clear sample of input and how this should looks like after applying some code. Thanks!

Change position of element in list python

a[i],a[j]=a[j],a[i] i don't know it's inner workings but it takes less runtime than yours and I can't say anything about memory as well ,wait for others explanation:-)

Change position of element in list python

Faheem Hossain , do you mean something like this: input: [1,2,3,4,5,6] output [1,[2,3],4,5,6]

Change position of element in list python

Abhay You are telling about swapping. But its not swapping, i want to move ith element to j and move all elements inside i and j to one place right. Thanks! I was wanting an algorithm which will do the above and be efficient like your one (swapping).

Change position of element in list python

David Ashton Lothar Input : [1,2,3,4,5] Let i = 3, j = 1 Output : [1,4,2,3,5] My code : A.insert(j, A.pop(i)) #i gave this in the question and i repeat that i dont know why, but its not that of a great use. I dont know its algorithm, but probably its running in a very bad runtime; please can you say why its bad and how i can increase its efficiency like one given by David Ashton 's one, but keeping the concept like one i exampled above.

Change position of element in list python

Faheem Hossain , thanks for your information, now its a bit more clear. But again, i still feel a bit uncomfortable with your statement "running in a very bad runtime." As long as you can't show as a concrete measurement comparing with other algorithms it looks a bit dubious for me.

Change position of element in list python

NotAPythonNinja I don't know its insights. Although it works but i don't know why, causes a great runtime!

Change position of element in list python

Change position of element in list python

David Ashton Lothar Thanks to everyone for your help! I hope this code will help you all to understand the problem clearly, and think of a better solution. Code: https://code.sololearn.com/cr6MgZwo82P1/?ref=app

Change position of element in list python

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

How to shift elements in a list in Python.
a_list = collections. deque([1, 2, 3, 4, 5]).
a_list. rotate(2) Shift `a_list` 2 places to the right..
shifted_list = list(a_list).
print(shifted_list).

Can you change elements in a list Python?

The easiest way to replace an item in a list is to use the Python indexing syntax . Indexing allows you to choose an element or range of elements in a list. With the assignment operator, you can change a value at a given position in a list.

How do you change the order of a list in Python?

Python lists can be reversed in-place with the list. reverse() method. This is a great option to reverse the order of a list (or any mutable sequence) in Python.

How do you move an item in a list to the end of the list in 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.