Move entry up or down the list in a collection?

I have a collection of 10 items. They are ordered [0,1,2,3,4,5,6,7,8,9]. How can I arbitrarily move an item up and down in a collection. Note I am not looking for list based solutions. A collection is not a list…right? I am still a little grey on the difference between a collection and a list so I just wanted to mention that.

My 10 items are actually 10 cameras with various properties from a class. I want to be able to re-order these entries based upon the selected index in the list.

Say I have entry #2 selected and I click the UP operator.

The operator needs to take [0,1,2,3,4,5,6,7,8,9] and convert it to [0,2,1,3,4,5,6,7,8,9].

What is the best ‘pythonic’ way to accomplish such a task. I realize I can do brute force list scanning and reconstruction but I wondered if there was some hidden feature already built into collections?

As always, thanks for your time!

As far as I know a list is a type of collection, but I’m not sure… How do you create it?

If you can read/write your collection by index number, this may work:

cameras[1], cameras[2] = cameras[2], cameras[1]

Where ‘cameras’ is the collection.

If this is not what you’re looking for I’m curious to see some of the code you have.

I assume you mean bpy.props.CollectionProperty. Pythonic swapping like in richard’s example doesn’t work for them.

There is a dedicated method: move()

It takes two integers, the source index and the target index.

To move the first entry “down”, you would use move(0, 1). To move it to the end instead, you would do use move(0, len(coll)-1) and so on.

See better answer below mine.

looks like this might be what your after

Thanks CoDEmanX, .move() is exactly what I was looking for.