Skip to content Skip to sidebar Skip to footer

Python 3.5 Iterate Through A List Of Dictionaries

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 m

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}]
forindex 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 = 0whileindex < 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}]
fordicin dataList:
    forkeyin 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:
    forvalin 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(*[valfor dic in dataList forvalin dic.values()], sep='\n')

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

Solution 2:

You can easily do this:

fordict_itemin dataList:
  forkeyin 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:

defextract_fullnames_as_string(list_of_dictionaries):
    
returnlist(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 = ([valfor dic in list_of_dictionaries forvalin 
    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))

Post a Comment for "Python 3.5 Iterate Through A List Of Dictionaries"