Python: Iterating over two lists and replacing elements in one list1 with the element from list2
Question: I have two lists of strings. In list1, which contains around 1000 string
elements, you have a string called "Date" that occurs randomly, immediately
followed by a string that contains the a particular Date : "17/09/2011". This
happens about 70 times. In List2: I have around 80 dates, as strings.

Question : I want to write a script that loops through both lists
simultaneously, and replaces the dates in list1, with the dates in list2, in
order. So, obviously you will have the first 70 dates of list2 replacing the
the 70 occurrences of dates in list1. Afterwards I want to write the modified
list1 to a .txt file.

I tried this, but I am totally stuck. I am super noob at Python.

    
    
    def pairwise(lst):
        """ yield item i and item i+1 in lst. e.g.
            (lst[0], lst[1]), (lst[1], lst[2]), ..., (lst[-1], None)
        """
        if not lst: return
        #yield None, lst[0]
        for i in range(len(lst)-1):
            yield lst[i], lst[i+1]
        yield lst[-1], None
    
    for line in file:
          list1.append(line.strip())
    for i,j in pairwise(list1):
         for k in list2:
         if i == "Date":
             list1."replace"(j) # Dont know what to do. And i know this double for looping is wrong also.
    


Answer: Maybe something like this (if there are no 'date' strings without a following
date):

    
    
    iter2 = iter (list2)
    for idx in (idx for idx, s in enumerate (list1) if s == 'Date'):
        list1 [idx + 1] = next (iter2)
    
    with open ('out.txt', 'w') as f:
        f.write ('{}'.format (list1) )
    

@user1998510, here a bit of explanation:

`enumerate` takes a list as an argument and generates tuples of the form (i,
i-th element of the list). In my generator (i.e. the `(x for y in z if a)`
part) I assign the parts of this tuple to the local variables idx and s. The
generator itself only yields the index as the actual item of the list (to whit
`s`) is of no importance, as in the generator itself we filter for interesting
items `if s == 'Date'`. In the `for` loop I iterate through this generator
assigning its yielded values to `idx` (this is another `idx` than the inner
`idx` as generators in python don't leak anymore their local variables). The
generator yields all the indices of the list whose element is 'Date' and the
`for` iterates over it. Hence I assign the next date from the second list to
the idx+1st item of the old list for all interesting indices.

