Python List/Array methods Flashcards
Adds an element at the end of the list
append()
Syntax
list.append(elmnt)
Parameter Values:
Parameter Description
elmnt Required. An element of any type (string, number, object etc.)
Definition and Usage
The append() method appends an element to the end of the list.
a = [“apple”, “banana”, “cherry”]
b = [“Ford”, “BMW”, “Volvo”]
a.append(b)
Removes all the elements from the list
clear()
Syntax
list.clear()
Parameter Values
No parameters
Definition and Usage
The clear() method removes all the elements from a list.
Example
Remove all elements from the fruits list:
fruits = [‘apple’, ‘banana’, ‘cherry’, ‘orange’]
fruits.clear()
Returns a copy of the list
copy()
Syntax
list.copy()
Parameter Values
No parameters
Definition and Usage
The copy() method returns a copy of the specified list.
Example
Copy the fruits list:
fruits = [‘apple’, ‘banana’, ‘cherry’, ‘orange’]
x = fruits.copy()
Returns the number of elements with the specified value
count()
Syntax
list.count(value)
Parameter Values
Required. Any type (string, number, list, tuple, etc.). The value to search for.
Definition and Usage
The count() method returns the number of elements with the specified value.
Example
Return the number of times the value “cherry” appears in the fruits list:
fruits = [‘apple’, ‘banana’, ‘cherry’]
x = fruits.count(“cherry”)
Add the elements of a list (or any iterable), to the end of the current list
extend()
Syntax
list.extend(iterable)
Parameter Values
Required. Any iterable (list, set, tuple, etc.)
Definition and Usage
The extend() method adds the specified list elements (or any iterable) to the end of the current list.
Example
Add a tuple to the fruits list:
fruits = [‘apple’, ‘banana’, ‘cherry’]
points = (1, 4, 5, 9)
fruits.extend(points)
Returns the index of the first element with the specified value
index()
Syntax
list.index(elmnt)
Parameter Values:
Parameter Description
elmnt - Required. Any type (string, number, list, etc.). The element to search for
Definition and Usage
The index() method returns the position at the first occurrence of the specified value.
Example
What is the position of the value 32:
fruits = [4, 55, 64, 32, 16, 32]
x = fruits.index(32)
Adds an element at the specified position
insert()
Syntax
list.insert(pos, elmnt)
Parameter Values:
Parameter Description
pos - Required. A number specifying in which position to insert the value
elmnt - Required. An element of any type (string, number, object etc.)
Definition and Usage
The insert() method inserts the specified value at the specified position.
Example
Insert the value “orange” as the second element of the fruit list:
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.insert(1, “orange”)
Removes the element at the specified position
pop()
Syntax
list.pop(pos)
Parameter Values:
Parameter Description
pos - Optional. A number specifying the position of the element you want to remove, default value is -1, which returns the last item
Definition and Usage
The pop() method removes the element at the specified position.
Example
Return the removed element:
fruits = [‘apple’, ‘banana’, ‘cherry’]
x = fruits.pop(1)
Removes the first item with the specified value
remove()
Syntax
list.remove(elmnt)
Parameter Values:
Parameter Description
elmnt - Required. Any type (string, number, list etc.) The element you want to remove
Definition and Usage
The remove() method removes the first occurrence of the element with the specified value.
Example
Remove the “banana” element of the fruit list:
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.remove(“banana”)
Reverses the order of the list
reverse()
Syntax
list.reverse()
Parameter Values:
No parameters
Definition and Usage
The reverse() method reverses the sorting order of the elements.
Example
Reverse the order of the fruit list:
fruits = [‘apple’, ‘banana’, ‘cherry’]
fruits.reverse()
Sorts the list
sort()
Syntax
list.sort(reverse=True|False, key=myFunc)
Parameter Values:
Parameter Description
reverse - Optional. reverse=True will sort the list descending. Default is reverse=False
key - Optional. A function to specify the sorting criteria(s)
Definition and Usage
The sort() method sorts the list ascending by default.
You can also make a function to decide the sorting criteria(s).
Example
Sort the list alphabetically:
cars = [‘Ford’, ‘BMW’, ‘Volvo’]
cars.sort()
What are python lists?
- Lists are used to store multiple items in a single variable.
- Lists are created using square brackets:
- List items are ordered, changeable, and allow duplicate values.
- List items are indexed, the first item has index [0], the second item has index [1] etc.
- To determine how many items a list has, use the len() function:
- 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.
How are lists created?
- Lists are created using square brackets
mylist = [“apple”, “banana”, “cherry”] - Using the list() constructor to make a List:
thislist = list((“apple”, “banana”, “cherry”)) # note the double round-brackets
What is an array in Python?
- 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.
- You refer to an array element by referring to the index number.
- Use the len() method to return the length of an array (the number of elements in an array).