Python Forum
how to print all data from all data array? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to print all data from all data array? (/thread-31388.html)



how to print all data from all data array? - korenron - Dec-08-2020

Hello,
maybe the names is incorrect so I will show in example
x = [{'id': '*1F010121', 'name': 'hotspot/Fonts', 'type': 'directory', 'creation-time': 'may/06/2018 09:35:12'},
{'id': '*1F010221', 'name': 'hotspot/images', 'type': 'directory', 'creation-time': 'may/06/2028 09:35:12'}]
}
I want to print all the names of the directory he found

something like
***** I KNOW THIS IS NOT THE CORRECT COMMAND
foreach data in x:
    print(x["name"][data])
so I will get
hotspot/Fonts , hotspot/images

Thanks ,


RE: how to print all data from all data array? - korenron - Dec-08-2020

Don;t know how to remove the post

But after playing I have found the answer:
for names in response:
    print(names['name'])
    List_OF_Names.append(names['name'])



RE: how to print all data from all data array? - Larz60+ - Dec-08-2020

I wrote a generic function to list dictionaries,
main() function shows how to display your data

def display_dict(dictname, level=0):
    indent = " " * (4 * level)
    for key, value in dictname.items():
        if isinstance(value, dict):
            print(f'\n{indent}{key}')
            level += 1
            self.display_dict(value, level)
        else:
            print(f'{indent}{key}: {value}')
        if level > 0:
            level -= 1


def main():
    x = [{'id': '*1F010121', 'name': 'hotspot/Fonts', 'type': 'directory', 'creation-time': 'may/06/2018 09:35:12'},
        {'id': '*1F010221', 'name': 'hotspot/images', 'type': 'directory', 'creation-time': 'may/06/2028 09:35:12'}]
    for item in x:
        print(display_dict(item))


if __name__ == '__main__':
    main()
Output:
id: *1F010121 name: hotspot/Fonts type: directory creation-time: may/06/2018 09:35:12 None id: *1F010221 name: hotspot/images type: directory creation-time: may/06/2028 09:35:12 None



RE: how to print all data from all data array? - korenron - Dec-30-2020

nice solution