简体   繁体   中英

How should I return dictionary one by one from list of dictionary in python

I have a list

list = [{'Details': [], 'age': None, 'his': F, 'Clear': None,
          'Computed': None, 'impact': 0,
          'Status': None, 'ID': None, 'bel': None,
          'Object': None, 'TicketId': 123, 'service': 257,
          'Status': 'NOT', 'Name': 'xyz'}, 
          
          {'Details': [], 'age': 56, 'his': F, 
           'Clear': DONE,
          'Computed': None, 'impact': 234,
          'Status': None, 'ID': None, 'bel': None,
          'Object': None, 'TicketId': 456, 'service': 650,
          'Status': 'NOT', 'Name': 'xyz'}]

In that list there is two dictionary and want to return those dictionaries one by one to other function of python, so what should I do?

I tried:

def data():
    iData = [{'Details': [], 'age': None, 'his': F, 'Clear': None,
              'Computed': None, 'impact': 0,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 123, 'service': 257,
              'Status': 'NOT', 'Name': 'xyz'}, 
              
              {'Details': [], 'age': 56, 'his': F, 
               'Clear': DONE,
              'Computed': None, 'impact': 234,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 456, 'service': 650,
              'Status': 'NOT', 'Name': 'xyz'}]

    

    for i in iData:
        return i

def return_data():
    i = data()
    print("returned i", i)

if __name__ == '__main__':
    return_data()

But when return statement hits, code come out of function, and returns only one dictionary, I want to return that both dictionaries one by one and need to do some operations in return_data() function.

You can use yield instead of return.

def data():
     iData = [{'Details': [], 'age': None, 'his': 'F', 'Clear': None,
              'Computed': None, 'impact': 0,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 123, 'service': 257,
              'Status': 'NOT', 'Name': 'xyz'}, 
              {'Details': [], 'age': 56, 'his': 'F', 
               'Clear': 'DONE',
              'Computed': None, 'impact': 234,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 456, 'service': 650,
              'Status': 'NOT', 'Name': 'xyz'}]

    

        for i in iData:
            yield i

   if __name__ == '__main__':
    result = data()
    print(next(result))
    print(next(result))

You can read more about generator at here , or check python docs on Genarator

You can use yield instead of return to create a generator object that contains the dictionaries

def data():
    iData = [{...}, {...}]

    for i in iData:
        yield i

def return_data():
    i = data()
    for x in i:
        print("returned i", x)

Output:

# returned i {'Details': [], 'age': None, 'his': 'F', 'Clear': None, 'Computed': None, 'impact': 0, 'Status': 'NOT', 'ID': None, 'bel': None, 'Object': None, 'TicketId': 123, 'service': 257, 'Name': 'xyz'}
# returned i {'Details': [], 'age': 56, 'his': 'F', 'Clear': 'DONE', 'Computed': None, 'impact': 234, 'Status': 'NOT', 'ID': None, 'bel': None, 'Object': None, 'TicketId': 456, 'service': 650, 'Name': 'xyz'}

As a side note, you can't have a duplicate key ('Status') in a dictionary.

You have three options that I know. First, try this

Example1

def data():
    return [{'Details': [], 'age': None, 'his': F, 'Clear': None,
              'Computed': None, 'impact': 0,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 123, 'service': 257,
              'Status': 'NOT', 'Name': 'xyz'}, 
              
              {'Details': [], 'age': 56, 'his': F, 
               'Clear': DONE,
              'Computed': None, 'impact': 234,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 456, 'service': 650,
              'Status': 'NOT', 'Name': 'xyz'}]

def return_data():
    i = data()
    print(f"returned i {[i[0],i[1]]}")

if __name__ == '__main__':
    return_data()

This above will return the list of dicts and you can use indexing to take each one.

Example2

def data():
    iData = [{'Details': [], 'age': None, 'his': F, 'Clear': None,
              'Computed': None, 'impact': 0,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 123, 'service': 257,
              'Status': 'NOT', 'Name': 'xyz'}, 
              
              {'Details': [], 'age': 56, 'his': F, 
               'Clear': DONE,
              'Computed': None, 'impact': 234,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 456, 'service': 650,
              'Status': 'NOT', 'Name': 'xyz'}]

    for i in iData:
        print(i)

def return_data():
    i = data()
if __name__ == '__main__':
    return_data()

Or you don't use return and instead use print.

Example3

Or you can call return data twice

def data():
    iData = [{'Details': [], 'age': None, 'his': F, 'Clear': None,
              'Computed': None, 'impact': 0,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 123, 'service': 257,
              'Status': 'NOT', 'Name': 'xyz'}, 
              
              {'Details': [], 'age': 56, 'his': F, 
               'Clear': DONE,
              'Computed': None, 'impact': 234,
              'Status': None, 'ID': None, 'bel': None,
              'Object': None, 'TicketId': 456, 'service': 650,
              'Status': 'NOT', 'Name': 'xyz'}]

    for i in iData:
        return return_data(i)

def return_data():
    i = data()
    print(f'data {i}')
if __name__ == '__main__':
    return_data()

In the for loop in the function data() you return i for every i in iData , the issue with this is that after the first pass the function returns and exits.

I would recommend moving the return .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM