1

my doubt is if it's possible order a list of element in python from strings in the index. for example

list = { }
list['b']='test1'
list['a']='test2'
list['c']='test3'

and i want to obtain this

list['a'] = 'test2'
list['b'] = 'test1'
list['c'] = 'test3'

Thanks in advance. Be patient but is my first day with python.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
CarCarlo
  • 13
  • 1
  • 6
  • 1
    what exactly do you want? Your list is a dictionary and not a list, and there is no way to order a dictionary. Are you sure you don't want list to actually be a list and not a dictionary? – Elias May 14 '14 at 21:58
  • Please read through the tutorial on dictionaries - https://docs.python.org/2/tutorial/datastructures.html#dictionaries. Note that, in Python, `list`s have indices, `dict`s have keys and (except in e.g. `numpy`) there are no `array`s. – jonrsharpe May 14 '14 at 22:05

1 Answers1

0

You cannot reference a block in the list with a string. what you can do is make a 2D list.

eg:

list = [['b', 'test1'],['a','test2'],['c','test3']]

this way list[0] = ['b', 'test1'] and list[0][0] = 'b'

not you can sort the list with the unicode values of a,b and c using the ord() function a simple bubble sort... python might even have a sort() function for

Makoto
  • 297
  • 1
  • 15