Loading lists in python

Writing List to a File in Python

In this article, you’ll learn how to write a list to a file in Python.

Often, we require storing a list, dictionary, or any in-memory data structure to persistent storage such as file or database so that we can reuse it whenever needed. For example, after analyzing data, you can store it in a file, and for the next time, that data can be read to use in an application.

There are multiple ways to write a Python list to a file. After reading this article, You’ll learn:

  • Write a list to a text file and read it in a Python program when required using a write() and read() method.
  • How to use Python’s pickle module to serialize and deserialize a list into a file. Serialize means saving a list object to a file. Deserialize means reading that list back into memory.
  • Use of built-in json module to write a list to a json file.

Table of contents

Steps to Write List to a File in Python

Python offers the write() method to write text into a file and the read() method to read a file. The below steps show how to save Python list line by line into a text file.

  1. Open file in write mode Pass file path and access mode w to the open() function. The access mode opens a file in write mode.
    For example, fp= open(r’File_Path’, ‘w’) .
  2. Iterate list using a for loop Use for loop to iterate each item from a list. We iterate the list so that we can write each item of a list into a file.
  3. Write current item into the file In each loop iteration, we get the current item from the list. Use the write(‘text’) method to write the current item to a file and move to the next iteration. we will repeat this step till the last item of a list.
  4. Close file after completing the write operation When we complete writing a list to a file, we need to ensure that the file will be closed properly. Use file close() method to close a file.

Example to Write List to a File in Python

# list of names names = ['Jessa', 'Eric', 'Bob'] # open file in write mode with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: for item in names: # write each item on a new line fp.write("%s\n" % item) print('Done')

Note: We used the \n in write() method to break the lines to write each item on a new line.

Below content got written in a file.

Text file after writing Python list into it

Example to Read List from a File in Python

Now, Let’s see how to read the same list from a file back into memory.

# empty list to read list from a file names = [] # open file and read the content in a list with open(r'E:\demos\files_demos\account\sales.txt', 'r') as fp: for line in fp: # remove linebreak from a current name # linebreak is the last character of each line x = line[:-1] # add current item to the list names.append(x) # display list print(names)

Write a list to file without using a loop

In this example, we are using a join method of a str class to add a newline after each item of a list and write it to a file.

The join() method will join all items in a list into a string, using a \n character as separator (which will add a new line after each list item).

# list of names names = ['Jessa', 'Eric', 'Bob'] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.write('\n'.join(names))

If you want to convert all items of a list to a string when writing then use the generator expression.

# list of names names = ['Jessa', 'Eric', 'Bob'] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.write("\n".join(str(item) for item in names))

Pickle module to write (serialize) list into a file

Python pickle module is used for serializing and de-serializing a Python object. For example, we can convert any Python objects such as list, dict into a character stream using pickling. This character stream contains all the information necessary to reconstruct the object in the future.

Читайте также:  Can read cookie php

Any object in Python can be pickled and saved in persistent storage such as database and file for later use.

Example: Pickle and write Python list into a file

  • First import the pickle module
  • To write a list to a binary file, use the access mode ‘b’ to open a file. For writing, it will be wb , and for reading, it will be rb . The file open() function will check if the file already exists and, if not, will create one. If a file already exists, it gets truncated, and new content will be added at the start of a file.
  • Next, The pickle’s dump(list, file_object) method converts an in-memory Python object into a bytes string and writes it to a binary file.
# Python program to store list to file using pickle module import pickle # write list to binary file def write_list(a_list): # store list in binary file so 'wb' mode with open('listfile', 'wb') as fp: pickle.dump(names, fp) print('Done writing list into a binary file') # Read list to memory def read_list(): # for reading also binary mode is important with open('sampleList', 'rb') as fp: n_list = pickle.load(fp) return n_list # list of names names = ['Jessa', 'Eric', 'Bob'] write_list(names) r_names = read_list() print('List is', r_names) 
Done writing list into a binary file List is ['Jessa', 'Eric', 'Bob']

Json module to write list into a JSON file

We can use it in the following cases.

  • Most of the time, when you execute a GET request, you receive a response in JSON format, and you can store JSON response in a file for future use or for an underlying system to use.
  • For example, you have data in a list, and you want to encode and store it in a file in the form of JSON.

In this example, we are going to use the Python json module to convert the list into JSON format and write it into a file using a json dump() method.

# Python program to store list to JSON file import json def write_list(a_list): print("Started writing list data into a json file") with open("names.json", "w") as fp: json.dump(a_list, fp) print("Done writing JSON data into .json file") # Read list to memory def read_list(): # for reading also binary mode is important with open('names.json', 'rb') as fp: n_list = json.load(fp) return n_list # assume you have the following list names = ['Jessa', 'Eric', 'Bob'] write_list(names) r_names = read_list() print('List is', r_names) 
Started writing list data into a json file Done writing JSON data into .json file List is ['Jessa', 'Eric', 'Bob']

json file after writing list into it

writelines() method to write a list of lines to a file

We can write multiple lines at once using the writelines() method. For example, we can pass a list of strings to add to the file. Use this method when you want to write a list into a file.

# list of names names = ['Jessa', 'Eric', 'Bob'] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.writelines(names)

Here is the output we are getting

cat sales.txt JessaEricBob

As you can see in the output, the file writelines() method doesn’t add any line separators after each list item.
To overcome this limitation, we can use list comprehension to add the new line character after each element in the list and then pass the list to the writelines method.

# list of names names = ['Jessa', 'Eric', 'Bob'] # add '\n' after each item of a list n_names = ["<>\n".format(i) for i in names] with open(r'E:/demos/files_demos/account/sales.txt', 'w') as fp: fp.writelines(n_names)
cat sales.txt Jessa Eric Bob

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

Читайте также:  cursor

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

Python Lists: A Complete Overview

Python Lists A Complete Overview Cover Image

In this tutorial, you’ll learn all you need to know to get started with Python lists. You’ll learn what lists are and how they can be used to store data. You’ll also learn how to access data from within lists by slicing and indexing data. You’ll learn how to add data to lists and as well as how to delete items. You’ll also learn how to create lists of lists, which can often be used to represent matrices of data.

What are Python Lists

Python lists are a data collection type, meaning that we can use them as containers for other values. One of the great things about Python is the simplicity of naming items. Think of lists as exactly that: lists. Lists in real life can contain items of different types and can even contain duplicate items.

Let’s take a look at some of the key properties of Python lists:

Python List Comprehensions Syntax

We create a new list and then evaluate an expression for each item in another iterable item.

For example, if we had a list that contained the values from 1 through 10, we could use a list comprehension to create a new list of all the squared values of these numbers.

Let’s see how we can do this with a list comprehension:

# A Simple List Comprehension Example numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] squares = [number ** 2 for number in numbers] print(squares) # Returns: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

We can see that this is a fairly readable approach to creating new lists. It can often be easier for beginners to read these and make sense of them than to write them. Because of this, if these don’t immediately make sense, don’t despair!

Читайте также:  How to style placeholder css

In the following section, you’ll learn more about Python lists of lists.

Python Lists of Lists

Because Python lists are simply container objects, they can actually contain other lists. This may sound a little odd at first, but the more you work with the language the more sense this will actually make.

You can actually think list of lists as being similar to matrices to hold data. Because lists themselves are ordered, this can be a logical way of storing data. For example, you can use each nested list to store information about different customers and use the outer list to store all the other lists. This saves you the effort of initializing new variables for each list.

Let’s see what this might look like:

# A Simple List of Lists customers = [ ['Nik', 32, 'Toronto'], ['Kate', 33, 'Paris'], ['Nate', 27, 'Nashville'], ]

Now, each item in our list customers is actually a list. Because of this, if we were to access the first item using the index of 0, we would actually simply return the first list.

# Accessing items in a list of lists customers = [ ['Nik', 32, 'Toronto'], ['Kate', 33, 'Paris'], ['Nate', 27, 'Nashville'], ] print(customers[0]) # Returns: ['Nik', 32, 'Toronto']

If we wanted to access the first item in our first list, we could simply chain our index accessors.

# Accessing the first item in our first sublist customers = [ ['Nik', 32, 'Toronto'], ['Kate', 33, 'Paris'], ['Nate', 27, 'Nashville'], ] print(customers[0][0]) # Returns: Nik

Lists of lists are one way to store data. They aren’t however, the best way to store large amounts of tabular data. For this, you may want to look toward using Numpy and Pandas, which you’ll learn about in future tutorials.

Conclusion and Recap

Phew! This was a long tutorial! You learned about the Python list data structure and its different attributes. You then learned how to work with lists, such as checking for membership, adding and removing items, and accessing data within lists.

Below, you’ll find a quick recap of Python lists:

  • Python lists are heterogenous, mutable, ordered data structures
  • They can be created using the list() function or square-brackets []
  • Because they’re ordered and indexed, you can access individual items using the [index] indexing method, or multiply items using slicing
  • You can add items using the .append() and .extend() methods and you can remove items using the .remove() and .pop() methods
  • You can loop over lists using simple for loops and you can use list comprehensions to easily create lists in a concise manner
  • Finally, you can use lists of lists to store more complex data structures, such as those meant to be represented in a tabular way

Next, you’ll learn about how to use Python conditions and conditional expressions to further modify your Python programs!

Additional Resources

Check out these related tutorials to learn about related topics:

Источник

Оцените статью