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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:54:37+00:00 2026-06-10T03:54:37+00:00

def big(dict, n): line = [] for k in dict: if k > n:

  • 0
def big(dict, n):
    line = []

    for k in dict:
       if k > n:
            line.append(k)
            return line

I have to find all the elements in dict larger than n.

However, my code only returns the largest number in dict larger than n.

What do I need to do in order to make it correct?

  • 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-10T03:54:39+00:00Added an answer on June 10, 2026 at 3:54 am

    The return line is tabbed too far over, so it returns when the first key larger than n is found (Note: a dictionary isn’t ordered by the way you write it), rather than going over all keys before returning. Try:

    def big(dic, n):
        line = []
        for k in dic:
            if k > n:
                line.append(k)
        return line
    

    In fact, you might prefer it to use list comprehension (and the function becomes just one line).

    def big(dic, n):
        return [k for k in dic if k>n]
    

    .

    Dictionaries compomise of key value pairs, {key: value} and when we iterate over a dictionary we are iterating over it’s keys. This explains the use of the variable k to iterate over the keys. That is,

    [k for k in dic] = [key1, key2, ...]
    

    Hence, if you want to find that with the largest value in the dictionary, you can use:

        return [dic[k] for k in dic if dic[k]>n]
    

    Note: I’ve changed the variable name to dic since (as @AndrewJaffe mentions) dict is a built-in object, and renaming it here may cause unexpected things to occur, and is generally considered bad practise. For example, if you wanted to check type(dic)==dict.

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

Sidebar

Related Questions

Here is my code. @@inversions = 0 numbers = [very big array] def merge_sort(array)
I have a big CSS file with all CSS we need for our internal
def size(number): if number<100: return Small() if number<1000: return Medium() return Big() def size1(number):
def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def filter(f, lst): if lst == []: return [] if f(lst[0]): return [lst[0]] +
so I have this big method in my application for newsletter distribution. Method is
I have a big problem (even my server's admin seems to have some difficulties
I have a big .py file, and I want Python to ignore everything after,

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.