Python 3.5 iterate through a list of dictionaries

141,272

Solution 1

You could just iterate over the indices of the range of the len of your list:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for index in range(len(dataList)):
    for key in dataList[index]:
        print(dataList[index][key])

or you could use a while loop with an index counter:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
index = 0
while index < len(dataList):
    for key in dataList[index]:
        print(dataList[index][key])
    index += 1

you could even just iterate over the elements in the list directly:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
    for key in dic:
        print(dic[key])

It could be even without any lookups by just iterating over the values of the dictionaries:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
    for val in dic.values():
        print(val)

Or wrap the iterations inside a list-comprehension or a generator and unpack them later:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
print(*[val for dic in dataList for val in dic.values()], sep='\n')

the possibilities are endless. It's a matter of choice what you prefer.

Solution 2

You can easily do this:

for dict_item in dataList:
  for key in dict_item:
    print dict_item[key]

It will iterate over the list, and for each dictionary in the list, it will iterate over the keys and print its values.

Solution 3

use=[{'id': 29207858, 'isbn': '1632168146', 'isbn13': '9781632168146', 'ratings_count': 0}]
for dic in use:
    for val,cal in dic.items():
        print(f'{val} is {cal}')

Solution 4

def extract_fullnames_as_string(list_of_dictionaries):
    
return list(map(lambda e : "{} {}".format(e['first'],e['last']),list_of_dictionaries))


names = [{'first': 'Zhibekchach', 'last': 'Myrzaeva'}, {'first': 'Gulbara', 'last': 'Zholdoshova'}]
print(extract_fullnames_as_string(names))

#Well...the shortest way (1 line only) in Python to extract data from the list of dictionaries is using lambda form and map together. 

Solution 5

"""The approach that offers the most flexibility and just seems more dynamically appropriate to me is as follows:"""

Loop thru list in a Function called.....

def extract_fullnames_as_string(list_of_dictionaries):

    result = ([val for dic in list_of_dictionaries for val in 
    dic.values()])

    return ('My Dictionary List is ='result)


    dataList = [{'first': 3, 'last': 4}, {'first': 5, 'last': 7},{'first': 
    15, 'last': 9},{'first': 51, 'last': 71},{'first': 53, 'last': 79}]
    
    print(extract_fullnames_as_string(dataList))

"""This way, the Datalist can be any format of a Dictionary you throw at it, otherwise you can end up dealing with format issues, I found. Try the following and it will still works......."""

    dataList1 = [{'a': 1}, {'b': 3}, {'c': 5}]
    dataList2 = [{'first': 'Zhibekchach', 'last': 'Myrzaeva'}, {'first': 
    'Gulbara', 'last': 'Zholdoshova'}]

    print(extract_fullnames_as_string(dataList1))
    print(extract_fullnames_as_string(dataList2))
Share:
141,272
C. P. Wagner
Author by

C. P. Wagner

Updated on December 08, 2021

Comments

  • C. P. Wagner
    C. P. Wagner over 2 years

    My code is

    index = 0
    for key in dataList[index]:
        print(dataList[index][key])
    

    Seems to work fine for printing the values of dictionary keys for index = 0.

    But for the life of me I can't figure out how to put this for loop inside a for loop that iterates through the unknown number of dictionaries in dataList