Week 6 - Array and List
Week 6 - Array and List
Array/List in Python
Introduction to List
Outline Initializing a List
List Method
Practice I
Practice II
Practice III
Submission
Introduction to Lists
What is a List? A List is a built-in data type in
Python.
Lists are used to store multiple items in a
single variable. 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.
Why list and not array?
A list and an array have several similarities, the major difference is the data type that can be stored
inside. A list can contain different data types, meanwhile an array must contain the same data type. Lists
are more popular, on the other side arrays are optimized for arithmetic operations. The downside of
using arrays is Python doesn’t include them out of the box, you should import them from a library.
Imagining a list
Imagine a list as a train that has many carriages. Each carriage holds a certain data that can be accessed
easily. Each carriage has an index that is used as the access key. The index of a list starts at 0, so the first
carriage has the index 0. Take a look at the illustration below. It is a list of car brands.
0 1 2 n
...
“Toyota” “BMW” “Volvo” data
Initializing a list
How to initialize a list?
Using the list example from the previous slide, a list can be initialized with its items as shown below.
Code Output
List methods
List methods
Below are list methods that can be used to manipulate lists in python:
index() - Returns the index of the first element with the specified value
append()- Add an element to the end of the list
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
clear() - Removes all items 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
reverse() - Reverses the order of the list
sort() - Sort the list
Accessing a list item
index()
How to access a list item?
Using the list example from the previous slide, a list can be accessed as shown below. This code will
print out the data that has index 1 from the cars list, which is the second item on the list.
Code Output
How to access a list item?
You can also determine the position/index of a specific item in a list such as below.
Code Output
Practice I
Desired Output
For students who have odd student ID, create a list of 10 beverage brands from around the world and
print out the data using the index that is the last digit of your student ID.
For students who have even student ID, create a list of 10 food brands from around the world and print
out the data using the index that is the last digit of your student ID.
For example, the last digit of my student ID is 7, thus I need to print the data with index 7 from the list.
Adding new list items
append() & insert()
How to add new items to a list?
Utilizing the built-in method of append() and insert(), one can add more items into a list. See the
example below.
Code Output
Code Output
Practice II
Desired Output
Initialize a list with these items 'Toyota', 'BMW', 'Volvo', 'Volkswagen', 'Fiat'. Manipulate the list using
adding and deleting items methods such that the output matches the desired output.
For students who have odd student ID, try and match this output.
For students who have even student ID, try and match this output.
Copying list items
copy()
How to copy items from a list?
We can use the copy() method to return a copy of a specified list
Code
Output
Counting the amount of items in a list
count()
How to count the number of items in a list?
The count() method returns the number of elements with the specified value.
Code Output
Combining the items of two lists
extend()
How to combine the items of two lists?
The extend() method adds the specified list elements (or any iterable) to the end of the current list.
Code
Output
Reversing the list order
reverse()
How to reverse the items order in a list?
The reverse() method reverses the sorting order of the items in a list.
Code Output
Sorting the list order
sort()
How to sort the items order in a list?
The sort() method sorts the list ascending by default (alphabetically).
Code Output
Editing and slicing lists
How to edit and slice a list?
To edit an item inside a list, items can be accessed using their indexes and apply changes directly. To apply
more than one changes at once, we can use slicing to make it easier.
Code
For students who have even student ID, initialize a list with these items [0, 3, 4, 6, 9, 11, 13, 14, 16, 19] and
match this output using slicing.
Additional Practice
(Optional)
Please create a program that will check whether the item is already on the list or not based on your
input user, if so, then determine the price
make 2 lists for example Grocery_list and Price_list. Include at least 5 items in this list.
You may use conditional statement but make sure to work with list
Expected Result
Submission
Screenshot & Upload to eCampus
Screenshot your source codes and terminals and put them into a pdf file. After that upload them to
eCampus on Practice Week 11.
For practice I, practice II, practice III please screenshot your source code and terminal.
Questions?
References
Python Lists - w3schools