banner



Reviewer Have Several Keys and Values in Python

Introduction :

A python dictionary is a collection of cardinal-value pairs. All keys in a python lexicon should be unlike.No two keys can accept the same name. But for different keys, we can have the same 'value'. This tutorial is to prove you lot how we can find all keys in a python dictionary with the aforementioned value.

Nosotros will create one program that volition :

  1. Create one lexicon by taking the inputs from the user.
  2. The user volition enter one value to discover in the dictionary.
  3. Information technology will find_ if more than two keys have the same value_ entered by the user.
  4. The program will likewise print out the keys for that value if found.

Solve it by using a loop :

This is the simplest choice to await for anything in a dictionary. At first, the program will get the total list of cardinal-value pairs from the dictionary and so using one_ for loop_, it will compare each pair i by one.

We will take all inputs from the user, i.e. the user will enter the key-value pairs for the lexicon and as well the value.

Python program :

Before going into details, let me show you the python program :

                          #ane              given_dict              =              {              }              result_set              =              prepare              (              )              #2              total_values              =              int              (              input              (              "Enter total no of key-value pairs of the dictionary : "              )              )              #3              for              i              in              range              (total_values)              :              #4              key_value_str              =              input              (              "Enter central and value for index {} separated by ',' : "              .              format              (i)              )              #v              key_value              =              key_value_str.split(              ','              )              #half dozen              given_dict[key_value[              0              ]              ]              =              key_value[              1              ]              #vii              value_to_find              =              input              (              "Enter value to find in the dictionary : "              )              #8              dict_item_list              =              given_dict.items(              )              #9              for              particular              in              dict_item_list:              if              item[              1              ]              ==              value_to_find:              #x              result_set.add(item[              0              ]              )              #xi              print              (              "Following are the keys found for value '{}' : "              .              format              (value_to_find)              )              for              particular              in              result_set:              print              (item)                      

(The source code is too shared on Github hither):

python dictionary find keys with same value

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. givendict_ is an empty dictionary. We will take the inputs from the user and insert them to this dictionary.resultset_ is an empty gear up for storing the final keys.
  2. Ask the user to enter the total number of primal-value pairs for the dictionary. Read and shop it in totalvalues_ variable.
  3. Run ane for loop for totalvalues_ fourth dimension.
  4. Ask the user to enter primal and value for the current index separated by a comma and store information technology in keyvaluestr variable. For instance, if the user will enter 1,i, we will store 'i,1' string in keyvaluestr.
  5. Split the variable cardinalvaluestr by_ ,_ . Store the result in primalvalue_ variable. If the string was "one,1", after splitting, keyvalue_ will hold_ "one"_ in its 0th alphabetize and "i" in its 1st alphabetize position.
  6. Add the key-value pairs to the dictionary. Retrieve that the key is stored in the 0th alphabetize and the value is stored in the first index.
  7. Ask the user to enter the value to discover in the dictionary.Shop it in valuetonotice variable.
  8. Using_ items()_ function, get the listing of all key-value pairs of the dictionary. Store the list in valuetofind variable.
  9. Now, iterate all items in the listing one by one using a_ 'for loop'._
  10. For each item in the list, check if any value is equal to the user input value valuetodiscover. If yes, add together the key for that value to the final result set.
  11. Finally, using 1 for loop, print out the list of keys for the user input value.

python dictionary find key with same value

Sample Output :

python dictionary find keys with same value

Conclusion :

As you accept seen that using one loop, we tin can hands detect out all keys for a specific value in python. Yous can likewise modify the above programme to check for multiple values instead of just ane. Try to run the to a higher place programme using python 3 and drib one comment beneath if you accept any queries.

Yous might also like :

  • Python Dictionary
  • Python Dictionary methods
  • Python iii Ordered Lexicon (OrderedDict) with instance
  • How to delete a key from a python dictionary
  • Python plan to discover the sum of all values of a dictionary
  • How to create a dictionary from 2 lists in python

graymosedeted.blogspot.com

Source: https://www.codevscolor.com/python-dictionary-find-keys-with-the-same-value

0 Response to "Reviewer Have Several Keys and Values in Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel