Python Forum

Full Version: how to print all data from all data array?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ,
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'])
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
nice solution