Writing a List to a File in Python

– Firstly, open the file in write mode by passing the file path and access mode “w” to the open() function.

– Next, we have to use the “for” loop to iterate the list. In each iteration, we will get a list item that we need to write in the file using the write() method.

– After iterating through the whole list, we need to ensure that we have closed the file. For that, we use the close() method.