146

I'm trying to save a list of strings, so that it can be accessed later. How can it be achieved using pickle? An illustrative example could help.

segfault404
  • 281
  • 1
  • 11
Lewis
  • 1,485
  • 2
  • 11
  • 8
  • 5
    have you looked at the docs? https://docs.python.org/3/library/pickle.html#examples – Padraic Cunningham Aug 23 '14 at 17:01
  • The easiest method is `repr` with `eval` if you don't care about speed and security. – Daniel Aug 23 '14 at 17:02
  • 2
    What does the list contain? – Ry- Aug 23 '14 at 17:03
  • The list just contains strings – Lewis Aug 23 '14 at 17:07
  • 4
    Then [`json`](https://docs.python.org/3/library/json.html) is the lighter-weight option. – Martijn Pieters Aug 23 '14 at 17:10
  • I have to agree that `pickle` is the wrong choice for a list of strings -- it's needlessly Python-specific and insecure (as it can represent content containing code). `json` is a better choice if you want something portable and available in the standard library, or `msgpack` if you want something fast to parse and generate (and nearly as portable) but don't mind loading third-party tools. – Charles Duffy Aug 23 '14 at 19:23
  • ...that said, in a lot of cases, I'd just represent a list of strings on-disk with a NUL-delimited stream. Since Python's strings are Pascal-style rather than C-style, that's not perfect -- there are strings that Python can represent that C can't -- but it's trivial to read and manipulate even from languages like bash which don't have built-in JSON libraries. – Charles Duffy Aug 23 '14 at 19:24

1 Answers1

251

Pickling will serialize your list (convert it, and it's entries to a unique byte string), so you can save it to disk. You can also use pickle to retrieve your original list, loading from the saved file.

So, first build a list, then use pickle.dump to send it to a file...

Python 3.4.1 (default, May 21 2014, 12:39:51) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['I wish to complain about this parrot what I purchased not half an hour ago from this very boutique.', "Oh yes, the, uh, the Norwegian Blue...What's,uh...What's wrong with it?", "I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!", "No, no, 'e's uh,...he's resting."]
>>> 
>>> import pickle
>>> 
>>> with open('parrot.pkl', 'wb') as f:
...   pickle.dump(mylist, f)
... 
>>> 

Then quit and come back later… and open with pickle.load...

Python 3.4.1 (default, May 21 2014, 12:39:51) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> with open('parrot.pkl', 'rb') as f:
...   mynewlist = pickle.load(f)
... 
>>> mynewlist
['I wish to complain about this parrot what I purchased not half an hour ago from this very boutique.', "Oh yes, the, uh, the Norwegian Blue...What's,uh...What's wrong with it?", "I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!", "No, no, 'e's uh,...he's resting."]
>>>
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
  • What if the List is too big for your memory, so you don't want to read the entire list into memory at-once but want to read only a part of the list at a time. Is it possible to read only a partial list into memory through pickle? – Watt Nov 06 '20 at 07:33
  • @Watt: if it was pickled as a list, then no. If the list was converted to something that can be read in strides, then yes. – Mike McKerns Nov 06 '20 at 13:29