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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:33:25+00:00 2026-06-10T10:33:25+00:00

I need to have my dictionary keys in this format ‘/ cat/’ but i

  • 0

I need to have my dictionary keys in this format ‘/ cat/’ but i keep getting multiple forward slashes. Here is my code:

 # Defining the Digraph method #
 def digraphs(s):
      dictionary = {}
      count = 0;
      while count <= len(s):
          string = s[count:count + 2]
          count += 1
          dictionary[string] = s.count(string)
      for entry in dictionary:
          dictionary['/' + entry + '/'] = dictionary[entry]
          del dictionary[entry]
      print(dictionary)
 #--End of the Digraph Method---#

Here is my output:

i do this:

digraphs(‘my cat is in the hat’)

{'///in///': 1, '/// t///': 1, '/// c///': 1, '//s //': 1, '/my/': 1, '/n /': 1, '/e /': 1, '/ h/': 1, '////ha////': 1, '//////': 21, '/is/': 1, '///ca///': 1, '/he/': 1, '//th//': 1, '/t/': 3, '//at//': 2, '/t /': 1, '////y ////': 1, '/// i///': 2}
  • 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-10T10:33:26+00:00Added an answer on June 10, 2026 at 10:33 am

    In Python, you generally shouldn’t iterate over objects while modifying them. Instead of modifying your dictionary, make a new one:

    new_dict = {}
    
    for entry in dictionary:
        new_dict['/' + entry + '/'] = dictionary[entry]
    
    return new_dict
    

    Or more compactly (Python 2.7 and above):

    return {'/' + key + '/': val for key, val in dictionary.items()}
    

    An even better approach would be to skip creating your original dictionary in the first place:

    # Defining the Digraph method #
    def digraphs(s):
        dictionary = {}
    
        for count in range(len(s)):
            string = s[count:count + 2]
            dictionary['/' + string + '/'] = s.count(string)
    
        return dictionary
    #--End of the Digraph Method---#
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need for a dictionary with multiple keys of 2 different types
I have multiple threads who all need to write to the same Dictionary. I
I have a class derived from Dictionary. I need this class to simulate a
I have a dictionary which I need to keep updated with incoming data, after
I have a dictionary with ints for keys, and strings for values. I need
All, I have a dictionary with two keys and values. I need to extract
I have a dictionary with several values that I want to keep constant, but
I have one dictionary I need to save into a plist. The paletteDictionary always
I have a dictionary dict1['a'] = [ [1,2], [3,4] ] and need to generate
I have a simple Dictionary(of String, Object) that I need to iterate through and

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.