There are a number of built-in data structures you can choose from when it comes to implementing arrays in Python. In this section, you’ve focused on core language features and data structures included in the standard library.
If you’re willing to go beyond the Python standard library, then third-party packages like NumPy and pandas offer a wide range of fast array implementations for scientific computing and data science.
In this section you learned:
- To store arbitrary objects, potentially with mixed data types use a
list
or atuple
- When you need mutability choose a
list
- For numeric data where memory and performance is important select
array.array
- For textual data represented as Unicode characters use the built-in
str
- For a mutable string-like data structure use a
list
of characters - For storing a contiguous block of bytes use immutable
bytes
type or abytearray
In most cases, you should start out with a simple list
. You’ll only need to specialize later on if performance or storage space becomes an issue. Most of the time, using a general-purpose array data structure like list
gives you the fastest development speed and the most programming convenience.
Here are resources for further documentation about arrays:
- Built-in Types: Lists | Python Documentation
- Built-in Types: Tuples | Python Documentation
- collections – Container Datatypes: namedtuple() | Python Documentation
- array – Efficient arrays of numeric values | Python Documentation
- Built-in Types: Bytes Objects | Python Documentation
- Built-in Types: Bytearray Objects | Python Documentation
Here are additional Real Python resources about arrays:
- Look Ma, No For-Loops: Array Programming With NumPy - Real Python Article
- The Pandas DataFrame: Make Working With Data Delightful - Real Python Article
- Unicode in Python: Working With Character Encodings - Real Python Course
- Lists and Tuples in Python - Real Python Course
- Strings and Character Data in Python - Real Python Course
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
RobyB on Feb. 24, 2021
Thank you!