-4
{
    "id": "3xj1z9mKjaI",
    "title": "\u0d36\u0d3e\u0d2a \u0d35\u0d3e\u0d15\u0d4d\u0d15\u0d4d \u0d2a\u0d31\u0d1e\u0d4d\u0d1e\u0d3f\u0d1f\u0d4d\u0d1f\u0d41\u0d33\u0d4d\u0d33 \u0d09\u0d2e\u0d4d\u0d2e\u0d2e\u0d3e\u0d30\u0d4d\u200d \u0d05\u0d31\u0d3f\u0d2f\u0d23\u0d02 | \u0d15\u0d30\u0d1e\u0d4d\u0d1e\u0d4d \u0d2a\u0d4b\u0d15\u0d41\u0d02 | makkal mathapithakkal | marhaba media",
    "viewCount": {
        "text": "163509"
    }
}

Give a solution to get the value corresponding to ''text'' key

Mureinik
  • 297,002
  • 52
  • 306
  • 350
John Honai
  • 69
  • 5

3 Answers3

2

You can access a specific value in a dict by using the subscript operator ([]). If the value that keys refers to is a dict itself, you can continue using the subscript operator on it. In your case:

text_value = my_dict['viewCount']['text']
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

You can use the [] operator.

By using it on the dictionary, it will return the value attached to that key, the same way a method would do so. This means that you can get the returned value and put it inside a variable, use it in a method, etc...

Example :

dct = {
    "my_first_key": "my_first_value",
    "my_second_key": "my_second_value"
}

my_var = dct["my_first_key"] # This variable will contain the string : "my_first_value"

You can then replicate this for an "inside dictionary" :

dct = {
    "my_first_key": "my_first_value",
    "my_second_key": "my_second_value",
    "my_second_dict": {
        "my_third_key": "my_third_value"
    }
}

my_var = dct["my_first_key"] # This variable will contain the string : "my_first_value"

my_final_var = dct["my_second_dict"]["my_third_key"] # This variable will contain the string : "my_third_value"
Nephty
  • 140
  • 1
  • 7
0

I would recommend that you take a look at the official python documentation, for your question the working code would be:

print(name_of_your_dictionary['viewCount']['text'])
Ali Awan
  • 180
  • 10