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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:52:24+00:00 2026-05-27T13:52:24+00:00

I want to increment a variable and – if a particular condition is fulfilled

  • 0

I want to increment a variable and – if a particular condition is fulfilled – I want to assign the next element of an iterator to it. In both cases the result should be appended to a list.

Problem is, that the function only recognizes the values already in the iterator.

The input-data is a nested list.

import datetime as dt

dates_prices = [[dt.datetime(2008, 6, 3, 0, 0), 48.54],
                [dt.datetime(2008, 6, 6, 0, 0), 47.99]]

def fillDates(dates_prices):
    filled = []
    iter_data = iter(dates_prices)
    item = iter_data.next()
    filled.append(item)
    while True:
        item[0] += dt.timedelta(1)
        try:
            if item in dates_prices:
                item = iter_data.next()
            filled.append(item)
        except StopIteration:
            return filled

a = fillDates(dates_prices)
print a

The function should check, which dates are missing in the original nested list. It should add all missing dates together with the last known price-point, so the output shoud be this:

a =
[[dt.datetime(2008, 6, 3, 0, 0), 48.54], 
[dt.datetime(2008, 6, 4, 0, 0), 48.54], 
[dt.datetime(2008, 6, 5, 0, 0), 48.54], 
[dt.datetime(2008, 6, 6, 0, 0), 47.99]]

What did I miss?

EDIT:

I altered the function that it is working now by creating a seperate list of dates from the nested list “dates_prices” and applying the suggestion by Sevenforce.

However, I still don’t know why my first solution didn’t work. I guess something with the variable assignment was wrong. But I don’t know what.

This is the new function:

import datetime as dt

dates_prices = [[dt.datetime(2008, 6, 3, 0, 0), 48.54], [dt.datetime(2008, 6, 6, 0, 0), 47.99]]

def fillDates(dates_prices):
    filled = []
    dates = [x[0] for x in dates_prices] #added this list
    iter_data = iter(dates_prices)
    item = iter_data.next()
    filled.append(item[:])

    while item[0] < dates[-1]:
        item[0] += dt.timedelta(1)
        if item[0] in dates: #using the new list here
            item = iter_data.next()
        filled.append(item[:]) #added colon here
    return filled


a = fillDates(dates_prices)
print a
  • 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-27T13:52:25+00:00Added an answer on May 27, 2026 at 1:52 pm

    I am suspecting dates_prices is a nested list.

    You possibly want to add a copy of item to filter and not the same object. To do this, change the line filled.append(item) to filled.append(item[:]). This will prevent item[0] += dt.timedelta(1) from changing already appended values in filled.


    To answer your edit:

    • Another [:] was missing: iter_data = iter(dates_prices[:]) prevents changes in input dates_prices itself (by item[0] += dt.timedelta(1), btw this is still happening in your updated code). This led to if item in dates_prices always evaluate to True.

    • With above change if item in dates_prices will be always False since [dt.datetime(2008, 6, 6, 0, 0), 48.54] != datetime.datetime(2008, 6, 6, 0, 0), 47.99] and therefore leads in an endless loop.

    Another working version (edited):

    import datetime as dt
    import copy
    
    dates_prices = [[dt.datetime(2008, 6, 3, 0, 0), 48.54],
        [dt.datetime(2008, 6, 6, 0, 0), 47.99]]
    
    def fillDates(dates_prices):
        filled = []
        iter_data = iter(copy.deepcopy(dates_prices))  #to copy the datetime objects
        item = iter_data.next()
        filled.append(item[:])
        dates_idx = 1
        while dates_idx < len(dates_prices):
            item[0] += dt.timedelta(1)
            if item[0] == dates_prices[dates_idx][0]:
                item = iter_data.next()
                dates_idx += 1
            filled.append(item[:])
        return filled
    
    a = fillDates(dates_prices)
    print a
    

    But there is still room for improvement, like using a dictionary for dates_prices.

    @jsbueno:
    You are right. The thing to learn here is to use something like

    new_item = [item[0] + dt.timedelta(1), item[1]]
    

    I think.

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

Sidebar

Related Questions

I want to increment the value of a variable when a button is pressed
I want to increment a small subsection (variable) of an matrix [illustrative code below]
i found some freaky error. I want to increment a counter, but the variable
Basically I want to increment the name of the variable. What is the correct
I want to increment a cookie value every time a page is referenced even
I want to increment the value of i . The for loop does not
I have several files containing this line Release: X I want to increment X
I am currently building a webshop for my own where I want to increment
I want to write a stored procedure to increment the value of an int
The new table has an auto-increment that I want to use for the data

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.