9

I have a list that is made of class instances:

MyList = [<instance1>, <instance2>, <instance3>]

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

MyList = [<instance1>, <instance3>, <instance2>]

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).

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • 1
    see http://stackoverflow.com/questions/2493920/how-to-switch-position-of-two-items-in-a-python-list – laike9m Jan 22 '14 at 10:49
  • In general, your code looks fine. Note that if you want to swap two elements in a list, your can do it like this: `my_list[1], my_list[2] = my_list[2], my_list[1]`. There should be no issue doing this with a list of instances. Moving on from that, I think there must be something else that is wrong. Is the Error coming when you swap the elements, or later when you try and use `self.matriceCaracteristiques`? Is `nbConditionSortieLong` an integer? – three_pineapples Jan 22 '14 at 10:55
  • @laike9m thanks for your suggestion. The problem is that I don't want to swap the elements, but just moving some of them from one position to the other (for some reasons). I mean, if I have: `MyList = [1,2,3,4,5]` and I want the 5 to come after the 2, I cannot swap the elements `3` and `5` cause I would get this: `MyList = [1,2,5,4,3]` and instead I need to get this `MyList = [1,2,5,3,4]` – Matteo NNZ Jan 22 '14 at 11:12
  • @three_pineapples yes, but as I wrote to laike9m swap is not exactly the action I want to take. So, the error comes exactly when I execute the last instruction (the `insert` method to the list) and yes, `nbConditionSortieLong` is an integer and there's no way it can not to be :) – Matteo NNZ Jan 22 '14 at 11:15

3 Answers3

7

First, I didn't get the error you mentioned.
Second, it seems that you made a mistake in using insert, should be insert(1, c) rather than insert(c, 1), see docs

>>> d = [[1, 2], [5, 6], [3, 4]]
>>> c = d[1]
>>> d.remove(c)
>>> d
[[1, 2], [3, 4]]
>>> d.insert(c, 1)
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    d.insert(c, 1)
TypeError: 'list' object cannot be interpreted as an integer
>>> d.insert(1, c)
>>> d
[[1, 2], [5, 6], [3, 4]]
SummerEla
  • 1,902
  • 3
  • 26
  • 43
laike9m
  • 18,344
  • 20
  • 107
  • 140
  • Ok, this is really funny because I swear in my Python shell (when I was trying the method) I wrote `d.insert(1,c)` but I swapped them into the project :D Thanks a lot, now it works! – Matteo NNZ Jan 22 '14 at 11:31
7

What about:

MyList.insert(index_to_insert,MyList.pop(index_to_remove))
Bill Charlaftis
  • 349
  • 1
  • 3
  • 14
redg25
  • 121
  • 1
  • 4
-3
values[0], values[1] = values[1], values[0]

this work fine for me.

databingo
  • 9
  • 2