Home > Net >  How to index a list of dictionaries in python?
How to index a list of dictionaries in python?

Time:12-30

If I have a list of dictionaries in a python script, that I intend to later on dump in a JSON file as an array of objects, how can I index the keys of a specific dictionary within the list?

Example :

dict_list = [{"first_dict": "some_value"}, {"second_dict":"some_value"}, {"third_dict": "[element1,element2,element3]"}]

My intuitive solution was dict_list[-1][0] (to access the first key of the last dictionary in the list for example). This however gave me the following error:

IndexError: list index out of range

CodePudding user response:

the key inputted into the dictionary will pick the some value in the format dict = {0:some_value}

to find a specific value:

list_dictionary = [{"dict1":'value1'},{"dict2","value2"}]
value1 = list_dictionary[0]["dict1"]

the 'key' is what you have to use to find a value from a dictionary

Example:

dictionary = {0:value}
dictionary[0]

in this case it will work

but to pick the elements we will do

values = []

for dictionary in dict_list:
   for element in dictionary:
      values.append(dictionary[element])

Output:

['some_value', 'some_value', ['element1', 'element2', 'element3']]

CodePudding user response:

dict_list = [{"first_dict": "some_value"}, {"second_dict":"some_value"}, {"third_dict": ['element1','element2','element3']}]

If your dict look like this you can do as well

dict_list[-1]["third_dict"]

You can't access 'the first key' with a int since you have a dict You can get the first key with .keys() and then

dict_list[-1].keys()[0]

CodePudding user response:

By using dict_list[-1][0], you are trying to access a list with a list, which you do not have. You have a list with a dict key within a list.

Taking your example dict_list[-1][0]:

  • When you mention dict_list you are already "in the list".
  • The first index [-1] is referring to the last item of the list.
  • The second index would only be "usable" if the item mentioned in the previous index were a list. Hence the error.

Using:

dict_list=[{"first_dict": "some_value"}, {"second_dict":"some_value"},{"third_dict": [0,1,2]}]

to access the value of third_dict you need:

for value in list(dict_list[-1].values())[0]:
    print(value)

Output:

0
1
2

CodePudding user response:

If you have the following list of dictionaries:

dict_list = [{"key1":"val1", "key2":"val2"}, {"key10":"val10"}]

Then to access the last dictionary you'd indeed use dict_list[-1] but this returns a dictionary with is indexed using its keys and not numbers: dict_list[0]["key1"]

To only use numbers, you'd need to get a list of the keys first: list(dict_list[-1]). The first element of this list list(dict_list[-1])[0] would then be the first key "key10"

You can then use indices to access the first key of the last dictionary:

dict_list[-1][ list(dict_list[-1])[0] ]

However you'd be using the dictionary as a list, so maybe a list of lists would be better suited than a list of dictionaries.

CodePudding user response:

If you know the order of dictionary keys and you are using one of the latest python versions (key stays in same order), so:


dict_list = [
    {"first_dict": "some_value"}
    , {"second_dict":"some_value"}
    , {"third_dict": ["element1", "element2", "element3"]}
]

first_key = next(iter(dict_list[-1].keys()))

### OR: value

first_value = next(iter(dict_list[-1].values()))

### OR: both key and value

first_key, first_value = next(iter(dict_list[-1].items()))


print(first_key)
print(first_key, first_value)
print(first_value)
  • Related