Just like list
in R, Record
in OCaml or object
in JS, in Python, dict
is just a value, not a key-value, and the elements in it are key-value, that means both of those elements have name (because the dict
they live in will record their names) and don't means this dict
it self have a name.
So, if you need to record names about some dicts or what ever anything, you shouldn't use []
to package them, you should use {}
and within names you want to specify:
dict1 = {... something in dict1 ...}
dicta = {... something in dicta ...}
...
dicts_dc = {'dict1': dict1, 'dicta': dicta, ...}
# means:
#
# dicts_dc = {
# 'dict1': {... something in dict1 ...},
# 'dicta': {... something in dicta ...},
# ... }
#
Or, you can also package them by []
, but you should record your dicts' names inside every dict object, such as:
dict1 = {'name': 'dict1', ...}
dicta = {'name': 'dicta', ...}
...
dicts_ls = [dict1, dicta, ...]
# means:
#
# dicts_ls = [
# {'name': 'dict1', ...},
# {'name': 'dicta', ...},
# ...]
#
Then, for the way one, you can get their names or values by these:
# get names use:
[n for n in dicts_dc] # or:
[* dicts_dc] # or:
list (dicts_dc) # or:
[n for n,x in dicts_dc.items()]
# get values use:
[x for x in dicts_dc.values()] # or:
[* dicts_dc.values()] # or:
list (dicts_dc.values()) # or:
[x for n,x in dicts_dc.items()]
And for the way two, you can get their names by:
[x['name'] for x in dicts_ls]
You can see a live demo here.