Python Forum
I am confused with the key and value thing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am confused with the key and value thing
#1
responses = {}
polling_active = True

while polling_active:
    name = input("\nWhat is your name? ")
    response = input("Which mountain would you like to climb someday? ")

    responses[name] = response  # <<< I know this line stores the response in the dictionary, is it store the name as a key and response as a value?I am confused by it

    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        polling_active = False

print("\n--- Poll Results ---")
for name, response in responses.items():
    print(f"{name} would like to climb {response}.")
I know this line stores the response in the dictionary, is it stores the name as a key and response as a value?I am confused by it.
Reply
#2
Yes, it does - each name is key and corresponding response - value in the dict
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Yes. Keys are to dicts what indexes (indices) are to lists and tuples.
Reply
#4
Lista and arrays are indexed using an integer. Integer indexing is indexing that uses integer values starting at 0 and going up to the number of elments in the list minus 1. Elements appear in the order of the ordinal value of the integer. list_[0] is the first element, list_[1] the second and so on.

Dictionary are indexed using a key. The key can be any hashable type (essentially any non-mutable type). Often these are str objects, but you can use integers, floats, sets, tuples, object methods, functions, classes, ... Order of elements in a dictionary is not important. element[2] may not be "after" element[1] or "before" element[3]. When you print a dictionary, or list the dictionary keys()/values()/items() the order is the order the elements were added to the dictionary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,053 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  Need help i just started the whole thing gabriel789 16 3,178 Sep-12-2022, 08:04 PM
Last Post: snippsat
  i making a terminal sign up website thing Kenrichppython 1 1,698 Nov-04-2021, 03:57 AM
Last Post: bowlofred
  Pandas confused DPaul 6 2,539 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,679 Sep-15-2021, 09:45 AM
Last Post: snippsat
  use thing before self sylvanas 3 2,337 Jul-21-2021, 04:54 PM
Last Post: sylvanas
  Confused with 'flags' tester_V 10 4,889 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Not able to make a specific thing pause in pygame cooImanreebro 4 3,203 Dec-13-2020, 10:34 PM
Last Post: cooImanreebro
  Simple Tic Tac Toe but I'm confused Izith 1 2,184 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 2,999 Sep-14-2020, 09:27 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020