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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:00:32+00:00 2026-05-13T08:00:32+00:00

I’m working through exercises in Building Skills in Python , which to my knowledge

  • 0

I’m working through exercises in Building Skills in Python, which to my knowledge don’t have any published solutions.

In any case, I’m attempting to have a dictionary count the number of occurrences of a certain number in the original list, before duplicates are removed. For some reason, despite a number of variations on the theme below, I cant seem to increment the value for each of the ‘keys’ in the dictionary.

How could I code this with dictionaries?

dv = list()
# arbitrary sequence of numbers
seq = [2,4,5,2,4,6,3,8,9,3,7,2,47,2]

# dictionary counting number of occurances
seqDic = { }

for v in seq:
    i = 1
    dv.append(v)
    for i in range(len(dv)-1):
        if dv[i] == v:
            del dv[-1]
            seqDic.setdefault(v)
            currentCount = seqDic[v]
            currentCount += 1
            print currentCount # debug
            seqDic[v]=currentCount
print "orig:", seq
print "new: ", dv
print seqDic
  • 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-05-13T08:00:32+00:00Added an answer on May 13, 2026 at 8:00 am

    defaultdict is not dict (it’s a subclass, and may do too much of the work for you to help you learn via this exercise), so here’s a simple way to do it with plain dict:

    dv = list()
    # arbitrary sequence of numbers
    seq = [2,4,5,2,4,6,3,8,9,3,7,2,47,2]
    
    # dictionary counting number of occurances
    seqDic = { }
    
    for i in seq:
      if i in seqDic:
        seqDic[i] += 1
      else:
        dv.append(i)
        seqDic[i] = 1
    

    this simple approach works particularly well here because you need the if i in seqDic test anyway for the purpose of building dv as well as seqDic. Otherwise, simpler would be:

    for i in seq:
      seqDic[i] = 1 + seqDic.get(i, 0)
    

    using the handy method get of dict, which returns the second argument if the first is not a key in the dictionary. If you like this idea, here’s a solution that also builds dv:

    for i in seq:
      seqDic[i] = 1 + seqDic.get(i, 0)
      if seqDic[i] == 1: dv.append(i)
    

    Edit: If you don’t case about the order of items in dv (rather than wanting dv to be in the same order as the first occurrence of item in seq), then just using (after the simple version of the loop)

    dv = seqDic.keys()
    

    also works (in Python 2, where .keys returns a list), and so does

    dv = list(seqDic)
    

    which is fine in both Python 2 and Python 3. Under the same hypothesis (that you don’t care about the order of items in dv) there are also other good solutions, such as

    seqDic = dict.fromkeys(seq, 0)
    for i in seq: seqDic[i] += 1
    dv = list(seqDic)
    

    here, we first use the fromkeys class method of dictionaries to build a new dict which already has 0 as the value corresponding to each key, so we can then just increment each entry without such precautions as .get or membership checks.

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

Sidebar

Ask A Question

Stats

  • Questions 393k
  • Answers 393k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Doing a lot of pointer dereferences can be a performance… May 15, 2026 at 1:55 am
  • Editorial Team
    Editorial Team added an answer You can actually use the request object to determine if… May 15, 2026 at 1:55 am
  • Editorial Team
    Editorial Team added an answer It is not completely clear to me what you want… May 15, 2026 at 1:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.