Home > Software design >  How to return all dictionary values and keys from function
How to return all dictionary values and keys from function

Time:05-30

need some help..

I have data like:

data dict:

{'folder_id': 13, 'type': 'local', 'read': False, 'last_modification_date': 1653588426, 
'creation_date': 1653408009, 'status': 'completed', 'uuid': 'ebaacb26-654b-04de-b5b3- 
 337b5bb66eea9e74cdc5db8168c5', 'shared': False, 'user_permissions': 128, 'owner': 
'[email protected]', 'timezone': 'Europe/Brusel', 'rrules': 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TU', 
'starttime': '20220211T190000', 'enabled': True, 'control': True, 'live_results': 0, 'name': 
 '6666-Test-VM01', 'id': 286}

{'folder_id': 2, 'type': 'local', 'read': True, 'last_modification_date': 1653587386, 
'creation_date': 1653399006, 'status': 'completed', 'uuid': 'fbbfbca1-ba55-31cf-7de9- 
984ab3aa2ce13223a2ed8c1b45a7', 'shared': False, 'user_permissions': 128, 'owner': 
'[email protected]', 'timezone': 'Europe/Brusel', 'rrules': 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TU', 
'starttime': '20220215T163000', 'enabled': True, 'control': True, 'live_results': 0, 'name': 
'testvm-3', 'id': 83}

{'folder_id': 2, 'type': 'local', 'read': False, 'last_modification_date': 1653587306, 
 'creation_date': 1653348635, 'status': 'completed', 'uuid': '762ba2a8-9e73-957c-4934- 
 ae919c7148453dbdd9ea1e07b423', 'shared': False, 'user_permissions': 128, 'owner': 
'[email protected]', 'timezone': 'Europe/Brusel', 'rrules': 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TU', 
 'starttime': '20220209T023000', 'enabled': True, 'control': True, 'live_results': 0, 'name': 
 '111-Vm3000test', 'id': 264}

I'm trying to return dictionary with all data inside, but it's returning only first or last value, how to get all data from this dictionary, my code:

class Testclass():
    def __init__(self):
        self.resources = {}
    
    def get_data(self):
        for data in data_dict:
            self.resources['folder_id'] = data['folder_id']
            self.resources['name'] = data['name']
        return self.resources

output of this code:

{'folders_id': 13, 'name': '6666-Test-VM01'}

why i didint get all anothers?

Expected output:

{'folders_id': 13, 'name': '6666-Test-VM01'} 
{'folders_id': 2, 'name': 'testvm-3'} 
{'folders_id': 2, 'name': '111-Vm3000test'} 

CodePudding user response:

Assuming data_dict is a list of dictionaries, let's do the same for resources, you can do something like:

def __init__(self):
    self.resources = []

def get_data(self):
    for data in data_dict:
        new_dict = {}
        new_dict['folder_id'] = data['folder_id']
        new_dict['name'] = data['name']
        self.resources.append(new_dict)
    return self.resources
  • Related