pythonlistdictionary

How to get the first value in a Python dictionary


I have a dictionary like this:

myDict = {  
    'BigMeadow2_U4': (1609.32, 22076.38, 3.98),  
    'MooseRun': (57813.48, 750187.72, 231.25),  
    'Hwy14_2': (991.31, 21536.80, 6.47)  
}

How can I get the first value of each item in my dicitionary?

I want in the end a list:

myList = [1609.32,57813.48,991.31]

Solution

  • Try this way:

    my_list = [elem[0] for elem in your_dict.values()]
    

    Offtop: I think you shouldn't use camelcase, it isn't python way

    UPD: inspectorG4dget notes, that result won't be same. It's right. You should use collections.OrderedDict to implement this correctly.

    from collections import OrderedDict
    my_dict = OrderedDict({'BigMeadow2_U4': (1609.32, 22076.38, 3.98), 'MooseRun': (57813.48, 750187.72, 231.25), 'Hwy14_2': (991.31, 21536.80, 6.47) })