0% found this document useful (0 votes)
22 views

M7S6 Array Methods

The document discusses Python array methods. It lists common array methods like append(), clear(), copy(), count(), extend(), index(), insert(), pop(), remove(), reverse(), and sort(). It provides the syntax and parameters for each method and includes sample code and output to demonstrate how each method works on arrays. The document aims to help students understand what Python arrays are, why they are used, how to identify and use different array methods in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

M7S6 Array Methods

The document discusses Python array methods. It lists common array methods like append(), clear(), copy(), count(), extend(), index(), insert(), pop(), remove(), reverse(), and sort(). It provides the syntax and parameters for each method and includes sample code and output to demonstrate how each method works on arrays. The document aims to help students understand what Python arrays are, why they are used, how to identify and use different array methods in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Array Methods

Module 7 Subtopic 6

medium.com
At the end of this lesson, the students should be
able to:

1. Understand what Python Arrays are; “Before software can


2. Explain why we are using arrays in be reusable it first has
Python; to be usable.” – Ralph
3. Identify the different Array methods Johnson
and learn how it works;
4. Attempt to use the different Arrays
in Python Programming
Python has a set of built-in methods that you can use on
lists/arrays
• Adds an element at the end of the list
Sample Code:
Syntax: list.append(elmnt)
[Required. An element of any type (string, number object
etc.)]
Sample Code:

Sample Output:

Sample Output:
• Removes all the elements from the list
Syntax: list.clear()

Sample Output:
Sample Code:
• The copy() method returns a copy of the specified list.
Syntax: list.copy()

Sample Code: Sample Output:


• The count() method returns the number of elements
with the specific value.
Value – is required. Any type (string, number, list,
Syntax: list.count(value)
tuple, etc.). The value to search for.
Sample Code: Return the number of times the value 9 appears int
the list:

Sample Code:

Sample Output:
Sample Output:
Add a tuple to the fruits list:
Add a tuple to the fruits list:

• The extend() method adds the specified list elements


(or any iterable) to the end of the current list.
Iterable – is required. Any iterable (list, set, tuple, etc.)
Syntax: list.extend(iterable)
Sample Code: Add a tuple to the fruit list:
Sample Code:

Sample Output: Sample Output:


• The index() method returns the position at the first
occurrence of the specified value.
Elmnt – is required. Any type(string, number, list, etc.).
Syntax: list.index(elmnt)
The element to search for.
Sample Code: What is the position of the value 32:

Sample Code:

Sample Output:

Sample Output:
• The insert() method inserts the specified value at the
specified position.
Parameters:
Syntax: list.insert(pos, elmnt)
Pos – is required. A number specifying in which
Sample Code: position to insert the value.

Elmnt – is required. An element of any type (string,


number, object. Etc.

Sample Output:
• The pop() method removes the element at the
specified position.
Pos – Optional. A number specifying the position of
Syntax: list.pop(pos)
the element you want to remove, default value is -1,
which returns the last item.
Sample Code:

Sample Code:

Sample Output:

Sample Output:
• The remove() methods removes the first occurrence
of the element with the specified value.
Syntax: list.remove(elmnt)
Parameters:
Sample Code:
Elmnt – is required. Any type (string, number, list. Etc.)
The element you want to remove.

Sample Output:
• The reverse() method reverses the sorting order of the
elements.
Syntax: list.reverse()

Sample Code: Sample Output:


• The sort() method sorts the list ascending by default.
You can also make a function to decide the sorting
criteria(s).
Parameters:
Syntax: list.sort(reverse=True|False, key=myFunc) • Reverse – Optional. Reverse=True will sort the list
descending. Default is reverse=false.
Sample Code: • Key – Optional. A function to specify the sorting
criteria(s).
Sample Code:

Sample Output:

Sample Output:
1. Sort a list of dictionaries based on the "year" value of the dictionaries:

Sample Code: Sample Output:

[{'car': 'Mitsubishi', 'year': 2000}, {'car': 'Ford', 'year': 2005},


{'car': 'VW', 'year': 2011}, {'car': 'BMW', 'year': 2019}]
“The beautiful thing about learning is that nobody can take it away
from you.” – B.B. King
https://www.guru99.com/python-arrays.html
https://www.w3schools.com/python/python_arrays.asp
https://www.tutorialspoint.com/python/python_arrays.htm
https://www.geeksforgeeks.org/python-arrays/
https://docs.python.org/3/library/array.html
https://www.javatpoint.com/python-arrays
https://pythonguides.com/python-array/
https://www.tutorialbrain.com/python-arrays/
https://www.programiz.com/python-programming/array

You might also like