Python array vs list vs tuple
17th Dec 2019 5 minutes read Array vs. List in Python What's the Difference?Kateryna Koidan
Both lists and arrays are used to store data in Python. Moreover, both data structures allow indexing, slicing, and iterating. So what's the difference between an array and a list in Python? In this article, we'll explain in detail when to use a Python array vs. a list. Show Python has lots of different data structures with different features and functions. Its built-in data structures include lists, tuples, sets, and dictionaries. However, this is not an exhaustive list of the data structures available in Python. Some additional data structures can be imported from different modules or packages. An array data structure belongs to the "must-import" category. To use an array in Python, you'll need to import this data structure from the NumPy package or the array module. And that's the first difference between lists and arrays. Before diving deeper into the differences between these two data structures, let's review the features and functions of lists and arrays. What Is a List in Python?A list is a data structure that's built into Python and holds a collection of items. Lists have a number of important characteristics:
Lists are very easily created in Python: list = [3, 6, 9, 12] print(list) print(type(list)) [3, 6, 9, 12]Python lists are used just about everywhere, as they are a great tool for saving a sequence of items and iterating over it. What Is an Array in Python?An array is also a data structure that stores a collection of items. Like lists, arrays are ordered, mutable, enclosed in square brackets, and able to store non-unique items. But when it comes to the array's ability to store different data types, the answer is not as straightforward. It depends on the kind of array used. To use arrays in Python, you need to import either an array module or a NumPy package. The Python array module requires all array elements to be of the same type. Moreover, to create an array, you'll need to specify a value type. In the code below, the "i" signifies that all elements in array_1 are integers: array_1 = arr.array("i", [3, 6, 9, 12]) print(array_1) print(type(array_1)) array('i', [3, 6, 9, 12])On the other hand, NumPy arrays support different data types. To create a NumPy array, you only need to specify the items (enclosed in square brackets, of course): array_2 = np.array(["numbers", 3, 6, 9, 12]) print (array_2) print(type(array_2)) ['numbers' '3' '6' '9' '12']As you can see, array_2 contains one item of the string type (i.e., "numbers") and four integers. So What's the Difference?Now that we know their definitions and features, we can talk about the differences between lists and arrays in Python:
Of course, it's possible to do a mathematical operation with a list, but it's much less efficient: From the Python Data Structures in Practice course So, when should you use a list and when should you use an array?
Time to Practice Python Arrays and Lists!Great! Now you know the difference between an array and a list in Python. You also know which to choose for a sequence of items. Now it's time to practice! If you want to advance your understanding of data structures and practice 100+ interactive exercises, check out the LearnPython.com course Python Data Structures in Practice. It will help you feel like a pro when dealing with lists, nested lists, tuples, sets, and dictionaries. Tags:
|