Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:51:48+00:00 2026-06-06T03:51:48+00:00

Python neophyte here. I was wondering if someone could help with the KeyError I

  • 0

Python neophyte here. I was wondering if someone could help with the KeyError I am getting when using a dictionary for string interpolation in str.format.

dictionary = {'key1': 'val1', '1': 'val2'}

string1 = 'Interpolating {0[key1]}'.format(dictionary)
print string1

The above works fine and yields:

Interpolating val1

However doing the following:

dictionary = {'key1': 'val1', '1': 'val2'}

string2 = 'Interpolating {0[1]}'.format(dictionary)
print string2

results in:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    string2 = 'Interpolating {0[1]}'.format(dictionary)
KeyError: 1L

So the problem seems to be in the interpretation of the numeric key as a list index, IMHO. Is there any way to work around this? (i.e. convey that this is instead a dictionary key)

TIA and apologies if this question has been asked before(couldn’t find anything relevant with my search-fu).

Edit 1: The key is not numeric as was erroneously noted, earlier. Instead it is a string representation of a number – as was pointed out by BrenBarn.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:51:49+00:00Added an answer on June 6, 2026 at 3:51 am

    No. According to the documentation:

    Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings ’10’ or ‘:-]’) within a format string.

    So you can’t use strings consisting of numbers as dictionary keys in format strings.

    Note that your key isn’t numeric, and it’s not trying to use it as a list index. Your key is a string that happens to contain a digit character. What it’s trying to do is use the number 1 (not the string “1”) as a dictionary key. It will work if you use the number 1 as your dictionary key (i.e., make your dict {'key1': 'val1', 1: 'val2'}).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python newbie here: I'm writing a market simulation in Python using Pysage, and want
I'm still a neophyte Python programmer and I'm trying to do something that is
Admittedly, I'm a bash neophyte. I always want to reach for Python for my
Python regular expression I have a string that contains keywords but sometimes the keywords
Python newbie here and wanted to know if there was a way to combine
python newbie here. I'm writing the code to control an experiment that has multiple
Python noob using 2.7 on Windows. I'm working on making a hierarchy tree view
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
Python came pre-installed on my macbook and I have been slowly getting acquainted with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.