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

  • Home
  • SEARCH
  • 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 8739169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:52:43+00:00 2026-06-13T10:52:43+00:00

So I have a script like this for key, Value in mydictionary.iteritems(): if ‘Mammal’

  • 0

So I have a script like this

for key, Value in mydictionary.iteritems():
    if 'Mammal' in Value[1]: 
    #because the value is a list of 2 items and I want to get at the second one
        Value[1] = Value[1].strip('Mammal')

This code effectively removes Mammal from the beginning of the second item in the Value list. Now I want to make this nicer python looking with list comprehension so I came up with this but obviously is wrong…. Any help?

Value[1] = [Value[1].strip('Mammal') for Key, Value in mydictionary.iteritems() if 'Mammal' in Value[1] ]

Also, on the same lines, a list comprehension to list all the keys in this dictionary. I am having a hard time coming up with that one.

I came up with:

for key, Value in mydictionary.iteritems():
Templist.append(key)

but as a list comprehension I am thinking….but it doesn’t work 🙁

alist = [key for Key, Value in mydictionary.iteritems()]
  • 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-13T10:52:44+00:00Added an answer on June 13, 2026 at 10:52 am
    mydictionary = {
        1: [4, "ABC Mammal"],
        2: [8, "Mammal 123"],
        3: [15, "Bird (Not a Mammal)"]
    }
    
    mydictionary = {key: ([value[0], value[1].strip('Mammal')] if 'Mammal' in value[1] else value) for key, value in mydictionary.iteritems()}
    print mydictionary
    

    Output:

    {1: [4, 'ABC '], 2: [8, ' 123'], 3: [15, 'Bird (Not a Mammal)']}
    

    Although I wouldn’t call this objectively “nicer looking”, so the iterative method may be preferable.

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

Sidebar

Related Questions

I have an user-defined Project setting (KEY/VALUE) like this: LATEST_BUILD_NUMBER 2.2.2.2 In my Info.plist,
I have a array which contains values like this See below for script sample
I have a 400 million lines of unique key-value info that I would like
I have a WebMethod that looks like this which is used to populate a
I have a google map javascript link like: string GoogleURL = http://maps.google.com/maps?file=api&v=2&key=abcdefg I built
I have an array like this: $a = array( 'key1' => 'value1', 'key2' =>
When we use Javascript prompt Box something like this var ur_name = prompt(Enter Name);
I have a Java application communicating with a SQL database via a PHP script,
I have a script which sends individual events into Splunk, each event is essentially
Currently I am working on project like web analytic using rails 3.2.3 I have

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.