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 7768007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:48:45+00:00 2026-06-01T15:48:45+00:00

I want to format a dictionary for printing (Python 2.7.3), and the dictionary has

  • 0

I want to format a dictionary for printing (Python 2.7.3), and the dictionary has tuples as keys. With other types of keys I can do

>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W', 'altitude':100}
>>> 'Coordinates: {0[latitude]}, {0[longitude]}'.format(coord)
'Coordinates: 37.24N, -115.81W'

I tried the same but it does not work with tuple keys.

>>> a={(1,1):1.453, (1,2):2.967}
>>> a[1,1]
1.453
>>> 'Values: {0[1,1]}'.format(a)

Traceback (most recent call last):
  File "<pyshell#66>", line 1, in <module>
    'Values: {0[1,1]}'.format(a)
KeyError: '1,1'

Why? How I can refer to tuple keys in formatting string?

FOLLOW UP

It seems we can’t (see answer below). As agf quickly pointed out, Python can’t handle this (hope it will be implemented).
In the meantime, I managed to refer to tuple keys in format string with the following workaround:

my_tuple=(1,1)
b={str(x):a[x] for x in a} # converting tuple keys to string keys
('Values: {0[%s]}'%(str(my_tuple))).format(b) # using the tuple for formatting
  • 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-01T15:48:46+00:00Added an answer on June 1, 2026 at 3:48 pm

    Under Format String Syntax, field_name is described (emphasis mine):

    The field_name itself begins with an arg_name that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. 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. The arg_name can be followed by any number of index or attribute expressions. An expression of the form '.name' selects the named attribute using getattr(), while an expression of the form '[index]' does an index lookup using __getitem__().

    The grammar describes arg_name as:

    arg_name          ::=  [identifier | integer]
    

    where identifier is:

    identifier ::=  (letter|"_") (letter | digit | "_")*
    

    So a tuple is not a valid arg_name as it is neither an identifier or an integer, and can’t be an arbitrary dictionary key because string keys aren’t quoted.

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

Sidebar

Related Questions

I love the way Python can format a string with a dictionary: print %(key1)s
I want to format a date object so that I can display strings such
I want to format a datetime field but i can't figure out the syntactic.
Consider this dictionary format. {1:{'name':'chrome', 'author':'google', 'url':'http://www.google.com/' }, 2:{'name':'firefox','author':'mozilla','url':'http://www.mozilla.com/'}} I want to remove all
I want to format an int as a currency in C#, but with no
I want to format a NSDecimalNumber as a Dollars value ($1.50) but Im getting
i want to format date to string in hql select, for example i have
I want to format a number in indian format. for example, x= 123456 should
I want to format my numbers throughout the application in a consistent way, no
I want to format a number as a percent, with at least 2 digits

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.