'How to print specific key with key and value
A = {
"Ajit": {
"Place": "Bharasar",
"Age": "20"
},
"Deepika": {
"Place": "Mankuva",
"Age": "19"
}
}
I want to print specific key Ajit
with Ajit
's key it self like:
{
'Ajit': {
'Place': 'Bharasar',
'Age':'20'
}
}
Solution 1:[1]
Do you mean:
A = {"Ajit":{"Place":"Bharasar","Age":"20"}, "Deepika":{"Place":"Mankuva","Age":"19"}}
key = "Ajit"
print(f"{{'{key}': {A[key]}}}")
And please remember to put four spaces before your code samples to get proper formatting
Solution 2:[2]
You just create a new Dict like the following code:
A = {
"Ajit": {
"Place": "Bharasar",
"Age": "20"
},
"Deepika": {
"Place": "Mankuva",
"Age": "19"
}
}
B = {
"Ajit": A["Ajit"]
}
print(B)
Solution 3:[3]
If you want Ajit
value, you can get it with A["Ajit"]
However, if you want to have the key and its associated value, you can print
A = {
"Ajit": {
"Place": "Bharasar",
"Age": "20"
},
"Deepika": {
"Place": "Mankuva",
"Age": "19"
}
}
key = "Ajit"
print("{" + key + ":" + A[key] + "}")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Leonardo |
Solution 2 | AlveMonke |
Solution 3 | AlveMonke |