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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:26:26+00:00 2026-05-31T06:26:26+00:00

The solution is probably rather simple, but I just can’t figure it out. Here

  • 0

The solution is probably rather simple, but I just can’t figure it out. Here is the code, it’s a simple fibonacci number generator. The goal is to sum up all even fibonacci numbers below 4,000,000.

My approach is to first generate all fibonacci numbers below 4,000,000, and then either:
a) generate a new list (“even”) with the even ones (this works fine)
b) removing the odd ones from the list “all”

However, in the latter case, the output is, for reasons I don’t understand, this:
[2, 5, 8, 21, 34, 89, 144, 377, 610, 1597, 2584, 6765, 10946, 28657, 46368, 121393, 196418, 514229, 832040, 2178309, 3524578]

Any help is much appreciated. Thanks!

all = []
even = []

def fibonacci():
    a, b = 1, 2
    while a < 4000000:
        all.append(a)
        a, b = b, a + b
    print all

##Putting all the even fibonacci numbers in a different list and summing them up works fine
#    for i in all:
#        if i % 2 == 0:
#            even.append(i)
#    print even                   
#    print sum(even)


# But for some strange reason I can't figure out how to remove the odd numbers from the list
    for i in all:
        if i % 2 != 0:
            all.remove(i)

    print all
    print sum(all)

fibonacci()
  • 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-31T06:26:27+00:00Added an answer on May 31, 2026 at 6:26 am

    This is a “gotcha” situation: you’re removing items from a list while iterating the list, thus changing the list, causing your iteration to behave unexpectedly. Try this:

    ...
    # But for some strange reason I can't figure out how to remove the odd numbers from the list
        for i in all[:]:
            if i % 2 != 0:
                all.remove(i)
    ...
    

    This is what’s called “slice” notation, and causes you to iterate a throwaway copy of the list so that your iteration is not affected by the all.remove() calls.

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

Sidebar

Related Questions

Probably a long question for a simple solution, but here goes... I have a
This is probably a simple question but I can't seem to find the solution.
This is probably a no brainer but... I've just opened a solution in VS2008
This is blowing my mind because it's probably an easy solution, but I can't
I am wondering if someone could help me with some probably rather simple code,
How can I add months to the CURRENT_TIMESTAMP in SQL Server? The solution probably
This is probably easy but I am getting stuck: when I build a solution
I'm new with Objective-C, so there probably is a simple solution to this. I
I'm sure there's an easy answer here and i'm just over-complicating things, but i
I think this should be a rather simple thing to do, but I'm completely

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.