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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:42:00+00:00 2026-06-04T12:42:00+00:00

For a Python Dictionary with a tuple key, how can only the part of

  • 0

For a Python Dictionary with a tuple key, how can only the part of the dictionary with one of the tuple elements set to a single value be displayed. Then also looking to delete those elements.

For example, the dictionary

testTrak = {(0,1): '+', (0,2): '-', (1,1): '34.0', (1,2): 'test'}

and I want to list only the elements with the first number of the tuple = 1; that is, I want to list only (1,*) where the asterisk represents a wild card; hence, only
(1,1): '34.0' and (1,2): 'test' would be listed.

It seems maybe some kind of slicing should work but don’t see it.

  • 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-04T12:42:01+00:00Added an answer on June 4, 2026 at 12:42 pm

    Sorry, I misread your question. Here’s the solution you need for pretty much any Python version:

    >>> testTrak = {(0,1): '+', (0,2): '-', (1,1): '34.0', (1,2): 'test'}
    >>> dict([(key, value) for key, value in testTrak.items() if key[0] == 1])
    {(1, 2): 'test', (1, 1): '34.0'}
    

    In Python 2.7+ you can write the less verbose version:

    >>> testTrak = {(0,1): '+', (0,2): '-', (1,1): '34.0', (1,2): 'test'}
    >>> {key:value for key, value in testTrak.items() if key[0] == 1}
    {(1, 2): 'test', (1, 1): '34.0'}
    

    Excluding the items is just a matter negating the if clause, if this is what you asked in the comments:

    >>> testTrak = {(0,1): '+', (0,2): '-', (1,1): '34.0', (1,2): 'test'}
    >>> {key:value for key, value in testTrak.items() if key[0] != 1}
    {(0, 1): '+', (0, 2): '-'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Searching a Python dictionary based on the value first, to get a key output
While Using Python Dictionary DataStructure (which contains key-value pair) if i want to retrieve
I trying to print out a dictionary in Python: Dictionary = {Forename:Paul,Surname:Dinh} for Key,Value
What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary)
Assume mydict is a Python dictionary. mydict = {'a': 1, 'b': 2} I can
I have a Python list which holds pairs of key/value: l = [[1, 'A'],
Let's say that I have a Python dictionary, but the values are a tuple:
I've got this python dictionary mydict, containing arrays, here's what it looks like :
I have a python dictionary of type defaultdict(list) This dictionary is something like this:
I have data in a python dictionary that I'd like to store in a

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.