You can use a loop:
with open('your_file.txt', 'w') as f: for item in my_list: f.write("%s\n" % item)
In Python 2, you can also use
with open('your_file.txt', 'w') as f: for item in my_list: print >> f, item
If you’re keen on a single function call, at least remove the square brackets []
, so that the strings to be printed get made one at a time (a genexp rather than a listcomp) — no reason to take up all the memory required to materialize the whole list of strings.
Related Posts:
- Writing a list to a file with Python
- How to open a file using the open with statement
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- How to remove \n from a list element?
- How to open a file for both reading and writing?
- Python IOError: File not open for reading
- How do I copy a file in Python?
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Understanding slice notation
- Understanding slice notation
- Using “with open() as file” method, how to write more than once? [duplicate]
- Correct way to write line to file?
- How to read a file line-by-line into a list?
- How to convert list to string [duplicate]
- How do I get the number of elements in a list?
- Finding the index of an item in a list
- Confused by python file mode “w+”
- How to make a flat list out of a list of lists
- Print string to text file
- Removing duplicates in lists
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- File open and close in python
- TypeError: ‘float’ object is not subscriptable
- TypeError: ‘float’ object is not subscriptable
- Transpose/Unzip Function (inverse of zip)?
- ValueError : I/O operation on closed file
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Finding the average of a list
- Print a list in reverse order with range()?
- write() versus writelines() and concatenated strings
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- How do I copy a file in Python?
- How do I concatenate two lists in Python?
- Finding median of list in Python
- Python set to list
- Python List vs. Array – when to use?
- Finding and replacing elements in a list
- ValueError: max() arg is an empty sequence
- How can I remove a trailing newline?
- Append integer to beginning of list in Python
- List comprehension on a nested list?
- Union of two lists in Python
- Understanding slice notation
- Add list to set?
- What’s the idiomatic syntax for prepending to a short python list?
- How to remove an element from a list by index
- Apply function to each element of a list
- Converting Dictionary to List?
- How to deep copy a list?
- TypeError: list indices must be integers or slices, not str
- In Python, when to use a Dictionary, List or Set?
- Check if two unordered lists are equal
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- How can I compare two lists in python and return matches
- How do I check if a list is empty?
- How can I get list of values from dict?
- IndexError: list index out of range and python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- Importing variables from another file?
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How do you split a list into evenly sized chunks?
- How to iterate through two lists in parallel?
- Flask raises TemplateNotFound error even though template file exists
- How do I check if there are duplicates in a flat list?
- How to return dictionary keys as a list in Python?
- How to save a dictionary to a file?
- Defining lists as global variables in Python
- Subtracting two lists in Python
- How do you read a file into a list in Python?
- SSL error : routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
- Get the cartesian product of a series of lists?
- Writing string to a file on a new line every time
- Python 2: AttributeError: ‘list’ object has no attribute ‘strip’
- What does the “x for x in” syntax mean?
- How to make a set of lists
- .write not working in Python
- numpy-equivalent of list.pop?
- Create nice column output in python
- TypeError: only integer arrays with one element can be converted to an index 3
- Logical indexing with lists
- How does str(list) work?
- convert csv file to list of dictionaries
- Element-wise addition of 2 lists?
- How to split elements of a list?
- Find object in list that has attribute equal to some value (that meets any condition)
- Turn a single number into single digits Python
- How to find out whether a file is at its `eof`?
- Iterating through list of list in Python
- Python cant convert ‘list’ object to str error [closed]
- Get difference between two lists
- How is Python’s List Implemented?
- Sorting list based on values from another list
- Create a list with initial capacity in Python
- Find all files in a directory with extension .txt in Python