The idea is about merging iterables, which comes handy in many cases. Working with zip function in Python is pretty neat and easy. We can convert the zip object to a tuple then to a string and write the string to the file: f.write(str(tuple(zip(list_1,list_2)))) The following will be the contents of the file:Īlso, there is a shorter code instead of using the for loop. Now close the file and check the saved data. List_2 = įinally, use the for loop to iterate through lists in zip function and write the result in the file (after converting a tuple to string): for i in zip(list_1, list_2): Now let’s create two lists to zip together. If the file doesn’t exist, it will be created. Coming at you from the city of Davis music from the days of the pager. The genre of music played by West Coast Classics is Hip Hop, Rap. Use the following line: f = open("zipOutput.txt", "a+") West Coast Classics is a radio station featured in Grand Theft Auto V and GTA Online. The first step is to open a file (we will use the append mode so nothing of existing content will be deleted). To save the output from the zip function into a file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |