Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I place a variable inside of a dictionary?

I want to put an variable from a dictionary called Theo_Skutar_contact and put it inside of the value of another dictionary called all_contact_dictionary. This is my code and please help me figure this out as this is a project for my dad’s job. You type in a name and it searches for the contact. In this case I used Theodore Skutar.

all_contact_dictionary = {
    "First Last": First_Last_contact,
}

First_Last_contact = {
    "First Name": "First",
    "Nickname": "Nick",
    "Last Name": "Last",
    "Company": "WorkingInc",
}

while True:
  name_input = input("Type first and last name here with capitals. Don't type nicknames.  ")

  if name_input in all_contact_dictionary:
    print(all_contact_dictionary[name_input])
  else:
    print("Not found. Try again.")

In the first dictionary is where I want to link it in the second value.

I tried doing ", i tried no ", i tried ‘, and nothing worked.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is this what you are trying to do?

Although I would suggest learning about Classes and Objects going forwards from here for this.

theo_skutar_contact = {
    "First Name": "Theodore",
    "Nickname": "Theo",
    "Last Name": "Skutar",
    "Company": "SomeCompany",
}

dave_murray_contact = {
    "First Name": "David",
    "Nickname": "Dave",
    "Last Name": "Murray",
    "Company": "Iron Maiden",
}

all_contact_dictionary = {
    "Theodore Skutar": theo_skutar,
    "David Murray": dave_murray_contact,
}

while True:
  name_input = input("Type first and last name here with capitals. Don't type nicknames.  ")

  if name_input in all_contact_dictionary:
    print(all_contact_dictionary[name_input])
  else:
    print("Not found. Try again.")

>> {'First Name': 'Theodore', 'Nickname': 'Theo', 'Last Name': 'Skutar', 'Company': 'SomeCompany'}

An example of using a dataclass

from dataclasses import dataclass

@dataclass
class Contact:
    first_name: str
    last_name: str
    nickname: str 
    company: str

theo_skutar = Contact(first_name = "Theodore", last_name = "Skutar", nickname= "Theo", company = "CompanyA")
dave_murray = Contact(first_name = "David", last_name = "Murray", nickname= "Dave", company = "Iron Maiden")


all_contact_dictionary = {
    "Theodore Skutar": theo_skutar,
    "David Murray": dave_murray,
}


while True:
  name_input = input("Type first and last name here with capitals. Don't type nicknames.  ")

  if name_input in all_contact_dictionary:
    print(all_contact_dictionary[name_input])
    print(all_contact_dictionary[name_input].nickname)
  else:
    print("Not found. Try again.")

>>> Contact(first_name='Theodore', last_name='Skutar', nickname='Theo', company='CompanyA')
>>> Theo
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading