Python Forum
A Dictionary in a Dictionary Syntax
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Dictionary in a Dictionary Syntax
#1
I'm getting a syntax error for the following code that I copied form a text book:

Code:

users = {
    "aeinstein": {
        "first": "albert",
        "last": "einstein",
        "location": "princeton"
        },
    
    "mccurie": {
        "first": "marie",
        "last": "curie",
        "location": "paris",
        },
    }

for username, user_info in users.items():
    print("f\nUsername: {username}")
    full_name = f"{user_info["first"]} {user_info["last"]}"
    location = user_info["location"]
    
    print(f"\tFull name: {full_name.title()}")
    print("f\tlocation: {location.title()}")
Here is the line that Python (Spyder) doesnt't like:

full_name = f"{user_info["first"]} {user_info["last"]}"
Reply
#2
You need to either use triple quotes or single quotes for the string (or the keys). Otherwise Python will try to end the string at the second double quote it finds, so the line would ne syntactically invalid.
Reply
#3
If I only change the keys the single quotes( 'aeinstein' and 'mccurie') I'm still getting an error message in the full_name line though, or do I need to change in other parts of the code?
Reply
#4
Problem is that you have strings within a string. Python can't tell where one begins and the other one ends. you have three ways to fix this.
1. change the outer string from double quotes to single quotes.
    full_name = f'{user_info["first"]} {user_info["last"]}'
2. change the inner strings to use single quotes
    full_name = f"{user_info['first']} {user_info['last']}"
3. use the format() method
    full_name = "{} {}".format(user_info["first"],user_info["last"])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Access individual items of a dictionary by clicking a button AdeS 7 160 Mar-13-2026, 04:36 PM
Last Post: woooee
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  python env with dictionary maiya 2 2,792 Mar-22-2025, 02:07 PM
Last Post: EmilyJohnson
  Replace values in Yaml file with value in dictionary PelleH 1 3,818 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Building a dictionary .update entry from variables Curbie 5 2,548 Sep-03-2024, 07:31 PM
Last Post: Curbie
  Dictionary using path. Bobbee 5 1,883 Aug-22-2024, 08:43 PM
Last Post: Bobbee
  Modifying a dictionary recursively SpongeB0B 2 1,913 May-12-2024, 04:09 PM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 2 2,287 Apr-29-2024, 04:38 PM
Last Post: Calab
Question Using Lists as Dictionary Values bfallert 8 3,938 Apr-21-2024, 06:55 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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