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?
- How do I copy a file 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?
- Python – TypeError: ‘int’ object is not iterable
- Understanding slice notation
- Understanding slice notation
- Using “with open() as file” method, how to write more than once? [duplicate]
- TypeError: list indices must be integers or slices, not str
- What is the difference between rw+ and r+
- Correct way to write line to file?
- IndexError: list index out of range and python
- How to read a file line-by-line into a list?
- Python – TypeError: ‘int’ object is not iterable
- How to convert list to string [duplicate]
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- 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+”
- Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- How to make a flat list out of a list of lists
- Print string to text file
- Removing duplicates in lists
- What does the list() function do in Python?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- File open and close in python
- Do I understand os.walk right?
- FileNotFoundError: [Errno 2] No such file or directory [duplicate]
- How to move a file in Python?
- TypeError: ‘float’ object is not subscriptable
- Is there a short contains function for lists?
- TypeError: ‘float’ object is not subscriptable
- Transpose/Unzip Function (inverse of zip)?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- FileNotFoundError: [Errno 2] No such file or directory
- How to move a file in Python?
- Check if something is (not) in a list in Python
- How do you append to a file?
- Python: finding an element in a list [duplicate]
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- ValueError : I/O operation on closed file
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Finding the average of a list
- Why do I get “Pickle – EOFError: Ran out of input” reading an empty file?
- How do I concatenate two lists in Python?
- if else in a list comprehension
- How can I randomly select an item from a list?
- Print a list in reverse order with range()?
- Get unique values from a list in python [duplicate]
- Fastest way to check if a value exists in a list
- write() versus writelines() and concatenated strings
- How can I count the occurrences of a list item?
- TypeError: ‘list’ object cannot be interpreted as an integer
- 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?
- if else in a list comprehension
- Finding median of list in Python
- Python set to list
- Python List vs. Array – when to use?
- How to find all occurrences of an element in a list
- Get list from pandas dataframe column or row?
- How can I remove a trailing newline?
- Finding and replacing elements in a list
- How to find all the indexes of a recurring item in a list?
- ValueError: max() arg is an empty sequence
- How can I remove a trailing newline?
- Remove list from list in Python
- python – if not in list
- How do I concatenate two lists in Python?
- How can I compare two lists in python and return matches
- How do I check whether a file exists without exceptions?
- How can I remove a trailing newline?
- Replace values in list using Python
- Call a function from another file?
- Append integer to beginning of list in Python
- List comprehension on a nested list?
- Union of two lists in Python
- Python: Array v. List
- Get unique values from a list in python
- ‘list’ object has no attribute ‘shape’
- How do I check if a list is empty?
- How can I check file size in Python?
- Python: Get the first character of the first string in a list?
- Understanding slice notation
- How do I find the duplicates in a list and create another list with them?
- Add list to set?
- What’s the idiomatic syntax for prepending to a short python list?