0

I have 2 functions below. When I loop though list_of_dicts in my dict_list() I am able to print out each dictionary in this list but when I return i in dict_list() and try to print out the return in get_dicts(i) it only returns / prints {'id': '33333', 'env': 'dev', 'cost': '250.00'} aka the first iteration of my loop. How can I return each dictionary so I can can then modify that data in get_dicts(i) something like if i['env'] == 'prod' i['env'] = "production" just an example but trying to avoid creating a list if possible.

def dict_list():
     list_of_dicts = [{'id': '12345', 'env': 'prod', 'cost': '50.00'}
                , {'id': '67890', 'env': 'dev', 'cost': '100.00'}
                , {'id': '11111', 'env': 'stage', 'cost': '150.00'}
                , {'id': '22222', 'env': 'prod', 'cost': '200.00'}
                , {'id': '33333', 'env': 'dev', 'cost': '250.00'}]
for i in list_of_dicts:
    print(i)
    return i

def get_dicts(i):
    print(i)
        

def main():
    i = dict_list()
    get_dicts(i)

if __name__ == '__main__':
    main()
  • 1
    Why do you want to avoid creating a list? That is typically how you return multiple values. Your only other option is to turn the function into a generator by using [`yield`](https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do) instead of `return` – 0x5453 Jul 01 '22 at 19:01
  • Welcome to stack overflow! When you use `i` as the iterator for your loop, it stores the last iteration when the loop ends. [Or on the first iteration if you return then. Edited because the indentation in the original question changed.] You then return it from your function. Either `yield` or just use the list of dicts in `get_dicts` and forget about `dict_list` altogether – G. Anderson Jul 01 '22 at 19:03
  • `return` inside `loop` will work only once – PM 77-1 Jul 01 '22 at 19:03
  • to answer the question of why I would like to avoid a list. I want to modify these dicts with a loop in my other function. something along the lines of: for value in i: if i['env'] == 'prod' i['env'] = "production" with a list of dictionaries I dont think I i can do that unless i pull all of my dicts out of that list. With lists I can only parse data by indexing and this will not scale when working with larger dicts – poppinpython Jul 01 '22 at 19:11
  • really sorry about the formatting I need to get the hang of stackoverflow comments – poppinpython Jul 01 '22 at 19:12
  • as per the second comment in this case yes I can just perform that operation in my fist function but that will not work with my actual data. In my actual data i am getting dicts from another function that is looping multiple dicts. They are not even in a list its just multiple dict outputs and from those I wanted to do some sorting and parsing so this example is as close as I can get to what I am having trouble with – poppinpython Jul 01 '22 at 19:17

1 Answers1

0
def dict_list():
    list_of_dicts = [{'id': '12345', 'env': 'prod', 'cost': '50.00'}
            , {'id': '67890', 'env': 'dev', 'cost': '100.00'}
            , {'id': '11111', 'env': 'stage', 'cost': '150.00'}
            , {'id': '22222', 'env': 'prod', 'cost': '200.00'}
            , {'id': '33333', 'env': 'dev', 'cost': '250.00'}]
    return list_of_dicts


def main():
    for dicts in dict_list():
        if dicts['env'] == 'prod':
             print(dicts)


if __name__ == '__main__':
    main()

have you try this method?

Lazuardi N Putra
  • 428
  • 1
  • 5
  • 15
  • that would work but I cant perform this loop in main it needs to be a stand alone function it seems I may just have to create a list if there is no other way. thank you for this suggestion though! – poppinpython Jul 01 '22 at 19:19