You can simply use a list to have a sorted list. If you want to link additional data, you can either use a tuple to store data, or even create a custom object for it that stores the identifier in an additional field.
You do not need to expand the list for this, you can just put any object in the list. For example, this would be easily possible:
>>> lst = [ ( 132, 'foobar' ), ( 58, 'other value' ) ]
>>> lst.append( ( 70, 'some data value' ) )
>>> lst
[(132, 'foobar'), (58, 'other value'), (70, 'some data value')]
>>> lst.sort( key=lambda x: x[0] )
>>> lst
[(58, 'other value'), (70, 'some data value'), (132, 'foobar')]
>>> lst.sort( key=lambda x: x[1] )
>>> lst
[(132, 'foobar'), (58, 'other value'), (70, 'some data value')]
edit:
If you are using Python 3.1+, you can also use a type collections.OrderedDict. This is an extension to normal dictthat maintains order as it does list.
source
share