reading and writing text files sidebar having retrieved the two lists we open the output file for writingand keep the file object in variable fh ("file handle"we then loop timesand in each iteration we create line to be written to the fileremembering to include newline at the end of every line we make no use of the loop variable iit is needed purely to satisfy the for in loop' syntax the preceding code snippetthe get_forenames_and_surnames(functionand an import statement constitute the entire program in the generate_test_names py program we paired items from two separate lists together into strings another way of combining items from two or more lists (or other iterablesis to use the zip(function the zip(function takes one or more iterables and returns an iterator that returns tuples the first tuple has the first item from every iterablethe second tuple the second item from every iterableand so onstopping as soon as one of the iterables is exhausted here is an examplefor in zip(range( )range( )range( ))print( ( ( ( ( although the iterators returned by the second and third range(calls can produce five items eachthe first can produce only fourso that limits the number of items zip(can return to four tuples here is modified version of the program to generate test namesthis time with each name occupying characters and followed by random year the program is called generate_test_names py and outputs the file test-names txt we have not shown the get_forenames_and_surnames(function or the open(call sinceapart from the output filenamethey are the same as before limit years list(range( ) for yearforenamesurname in ziprandom sample(yearslimit)random sample(forenameslimit)random sample(surnameslimit))name "{ { }format(forenamesurnamefh write("{ < { }\nformat(nameyear)we begin by setting limit on how many names we want to generate then we create list of years by making list of the years from to inclusiveand then replicating this list three times so that the final list has three occurrences of each year this is necessary because the random sample(function that we are using (instead of random choice()takes both an iterable and how