I have a python list which is coming from an API response where values are base64 encoded images, so the strings are super long. I want to show the structure and data types of the list without showing the values.
{
...
"api_response": [
[
[
"super long string of base64 encoded image",
"super long string of base64 encoded image",
"super long string of base64 encoded image",
"super long string of base64 encoded image",
]
]
]
}
When i try:
print(type(api_response))
The result is:
<class 'list'>
When I try:
print(api_response)
I get a giant blob of text which shows the base64 string. I want formatted and linted like:
[
[
[
<string>,
<string>,
<string>,
<string>,
]
]
]
CodePudding user response:
A list in python can hold data of multiple different datatypes.
You could use map
and type
on your list to get all datatypes like this:
l = ["a", 1, True]
list(map(type, l))
# [<class 'str'>, <class 'int'>, <class 'bool'>]
CodePudding user response:
If your data objects are at various depth within you list,
e.g.
l = [[["aa", "bb", 345], "cc"]]
I would do a recursive function like so:
def type_map(l):
if not isinstance(l, list):
return str(type(l))
else:
l = [type_map(x) for x in l]
return l
print(type_map(l))
outputs:
[[["<class 'str'>", "<class 'str'>", "<class 'int'>"], "<class 'str'>"]]
Then for pretty printing, I would use json formatter of json
module
import json
print (json.dumps(type_map(l), indent=2))
outputs:
[
[
[
"<class 'str'>",
"<class 'str'>",
"<class 'int'>"
],
"<class 'str'>"
]
]