Python Arrays – Python Lists
Does python have an array?
- Arrays are used to store multiple values in one single variable.
- Python does not have built-in support for Arrays, but Python Lists can be used instead.
What is Python Lists/Python Arrays?
Python Lists are used to store multiple items in a single variable. For example,
fruits_list has 4 items
apple, banana, cherry, orange
fruits_list = ["apple", "banana", "cherry", "orange"]
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets
Python List/Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
|append()||Adds an element at the end of the list|
|clear()||Removes all the elements from the list|
|copy()||Returns a copy of the list|
|count()||Returns the number of elements with the specified value|
|extend()||Add the elements of a list (or any iterable), to the end of the current list|
|index()||Returns the index of the first element with the specified value|
|insert()||Adds an element at the specified position|
|pop()||Removes the element at the specified position|
|remove()||Removes the first item with the specified value|
|reverse()||Reverses the order of the list|
|sort()||Sorts the list|
Python Lists/Python Arrays Items
- List items are ordered, changeable, and allow duplicate values.
- List items are indexed, the first item has index
, the second item has index
etc. For example,
fruits_list = ["apple", "banana", "cherry", "orange"]then
- When we say that lists are ordered, it means that the items have a defined order, and that order will not change.
- If you add new items to a list, the new items will be placed at the end of the list.
- Note: There are some list methods that will change the order, but in general: the order of the items will not change.
- The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
Python List Items Allow Duplicates
- Since lists are indexed, lists can have items with the same value:
fruits_list = ["apple", "banana", "cherry", "apple", "cherry"]
Python List Length
- To determine how many items a list has, use the
Print the number of items in the list:
fruits_list = ["apple", "banana", "cherry", "apple", "cherry"] len(fruits_list)
List Items – Data Types
- List items can be of any data type.
Example: String, int and boolean data types:
list1 = ["apple", "banana", "cherry"] list2 = [1, 5, 7, 9, 3] list3 = [True, False, False]
- A list can contain different data types:
Example: A list with strings, integers and boolean values:
my_list = ["abc", 34, True, 40, "male"]
- From Python’s perspective, lists are defined as objects with the data type ‘list’:
- <class ‘list’>
Example: What is the data type of a list?
mylist = ["apple", "banana", "cherry"] print(type(mylist))
The list() Constructor
- It is also possible to use the list() constructor when creating a new list.
list()constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets print(thislist)
Python List of Lists (Python List in list)
A list of lists in Python is a list object where each list element is a list by itself. Create a list of list in Python by using the square bracket notation to create a nested list
[[1, 2, 3], [4, 5, 6], [7, 8, 9]].
Create a List of Lists in Python
Create a list of lists by using the square bracket notation. For example, to create a list of lists of integer values, use
[[1, 2], [3, 4]]. Each list element of the outer list is a nested list itself.
Convert List of Lists to One List
Say, you want to convert a list of lists
[[1, 2], [3, 4]] into a single list
[1, 2, 3, 4]. How to achieve this? There are different options:
- List comprehension
[x for l in lst for x in l]assuming you have a list of lists
[*lst, *lst]assuming you have a list of two lists
- Using the
extend()method of Python lists to extend all lists in the list of lists.
Find examples of all three methods in the following code snippet:
lst = [[1, 2], [3, 4]] # Method 1: List Comprehension flat_1 = [x for l in lst for x in l] # Method 2: Unpacking flat_2 = [*lst, *lst] # Method 3: Extend Method flat_3 =  for l in lst: flat_3.extend(l) ## Check results: print(flat_1) # [1, 2, 3, 4] print(flat_2) # [1, 2, 3, 4] print(flat_3) # [1, 2, 3, 4]
Due its simplicity and efficiency, the first list comprehension method is superior to the other two methods.