Last answered:

21 Mar 2023

Posted on:

04 Nov 2022

1

key of Dictionary

what if you want to access the key of the value?

3 answers ( 0 marked as helpful)
Instructor
Posted on:

07 Nov 2022

0

Hey Osama,

Thank you for reaching out!

I believe there is no as straightforward way to get the key of a dictionary given a value. I suggest you go through the Python documentation, study the operations that the dictionary data type supports, and try to implement a function that does what you need.

Kind regards,
365 Hristina

Posted on:

21 Mar 2023

0

Hi, for dictionary in dictionary case ie when i input:

1) countries['France'] or countries["France"],i could retrieve the value of the dictionary

2) however when i want to extract the value of the nested dictionary, value['Capital'] does not work and it needs to be value["Capital"], may i understand why? 


many thanks!

Instructor
Posted on:

21 Mar 2023

1

Hey Chan,


Thank you for reaching out!


In general, single and double quotation marks serve the same purpose in Python, namely, to define a string. They can be used interchangeably as in the example below:

However, when quotation marks are used more than once in an expression, they need to be distinguished from one another. Study the following example:

Here, the single quotation marks denote the beginning of an f-string while the double quotation marks define the strings Capital and Language. In contrast, in the example below, the single quotation marks instead denote the beginning of an f-string while the double ones define the strings Capital and Language.

Finally, the following code returns a syntax error since the f-string begins in position 1 and ends in position 2:


Hope this helps!


Kind regards,

365 Hristina

Submit an answer