Lec 03 - List and Tuple
Lec 03 - List and Tuple
List + Tuple
List
List definition
Create a list
list() constructor
Check for the order in the list
List indexing
List slicing
Add/Change/Remove items in a list
Reverse list
Iterate list
List basic operations 2
1
Tuple
Tuple definition
Create a tuple
Create a tuple with one element
tuple() constructor
Tuple packing and unpacking
Tuple indexing
Tuple slicing
Change/Add/Remove tuple in a list
Tuple basic operations
3
List
A list is used to store the sequence of various types of data.
The data in a list are called items or elements.
2
List
Create a list:
It can be created by placing all the items (elements) inside
square brackets [], separated by commas.
list1 = [ ] #empty list
list2 = ["FIST", "FET", "FOB", "FOL"] #same data type
list3 = [1, 2, 'a', 'b', 4.6, 8.9 ] #mixed data type
list4 = ["Melaka", ['a', 'b'], [22, 66, 80]] #nested list
5
Picture source: https://www.dataquest.io/blog/python-list-tutorial/
List
list () constructor:
It can be used to create list or convert other sequence types
such as tuple, set, dictionary, string to list.
3
List
Check for the order in the list:
Lists are ordered – Lists remember the order of items inserted.
List
List indexing:
It is used to access elements in a list.
Index operator [] is used to access an item in a list.
Negative indexing - the index of -1 refers to the last item,
-2 to the second last item and so on.
8
Picture source: https://www.dataquest.io/blog/python-list-tutorial/
4
List
List indexing:
List
List slicing:
A range of items in a list can be assessed by using the slicing
operator :(colon).
start: the beginning index of the slice.
list_name(start:stop:step) stop: the ending index of the slice (not include ending index).
step: the amount by which the index increases, defaults to 1.
s w e a t e a t
existing list new list
5
List
Index: 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| s | e | c | u | r | i | t | y |
+---+---+---+---+---+---+---+---+
Negative index: -8 -7 -6 -5 -4 -3 -2 -1
11
List
Index: 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| s | e | c | u | r | i | t | y |
+---+---+---+---+---+---+---+---+
Negative index: -8 -7 -6 -5 -4 -3 -2 -1
12
6
List Index: 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| s | e | c | u | r | i | t | y |
+---+---+---+---+---+---+---+---+
Negative index: -8 -7 -6 -5 -4 -3 -2 -1
Index: 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| s | e | c | u | r | i | t | y |
+---+---+---+---+---+---+---+---+
Negative index: -8 -7 -6 -5 -4 -3 -2 -1
Index: 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| s | e | c | u | r | i | t | y |
+---+---+---+---+---+---+---+---+
Negative index: -8 -7 -6 -5 -4 -3 -2 -1
13
List
Add items in a list:
Lists are mutable – it means the elements can be changed.
New items are added using insert(), append() or extend()
methods.
insert() – to add specified item at the specified index value
append() – to add one item at the end of a list
extend() – to add several items into a list
Multiple new items are added by squeezing it into specific position.
14
7
List
Add items in a list:
15
List
Add items in a list:
Start and
stop must
be same
16
8
List
Change items in a list:
Element in a list can be changed using index.
Multiple items can be changed by assigning the slice to some
specific values.
list1= ['Jeff', ['Ali', 'Alan'], 'Muthu', 'Wong']
Start and
stop is
different
list2= ['Jeff', 6.2, 8.5, 9.7, 'Wong']
17
List
Remove items in a list:
Items can be removed from a list by using the keyword del.
The keyword del can even delete the list entirely.
Multiple items can be deleted by assigning the appropriate slice to
an empty list.
Items can be removed using remove(), pop() or clear() methods.
remove() – to remove the given item
pop()
It removes an item at the given index.
It removes and returns the last item if the index is not provided.
clear() – to empty a list
18
9
List
Remove items in a list: del keyword
Error message
shows the list is
not exist
19
List
Remove items in a list:
20
10
List
Reverse list:
List can be reversed using reverse() function.
It can be done also by list slicing (omitting both start and stop
indices and specifying a step as -1).
Iterate list:
A list items can be iterated using for loop.
21
List
List basic operations:
Python Expression Results Description
22
11
Tuple
A tuple is an ordered collection of items (elements).
Tuples are a lot like list:
Tuples are ordered
Accessed by index
Tuples can contain any sort of data type
Except:
Tuples are immutable
items can not be added, deleted, or changed after the tuple is
defined.
23
Tuple
Create a tuple:
It is created by placing all the items (elements) inside
parentheses (), separated by commas.
T1 = () #empty tuple
24
12
Tuple
Create a tuple with one element:
It is a bit tricky to create a tuple with one element.
A trailing comma is need to indicate that it is, in fact, a tuple.
25
Tuple
tuple() constructor:
tuple() constructor can be used to create tuple or convert other
sequence types such as list, set, dictionary, string to tuple.
26
13
Tuple
Tuple packing:
When a tuple is created, the items in the tuple are packed
together into the object.
Tuple unpacking:
When a packed tuple is assigned to a new tuple, the individual
items are unpacked (assigned to the items of a new tuple).
The number of variables on the left must match the number of
items in the tuple.
27
Tuple
Tuple indexing:
It is used to access elements in a list.
Index operator [] is used to access an item in a tuple.
Negative indexing - the index of -1 refers to the last item,
-2 to the second last item and so on.
28
14
Tuple
Tuple slicing:
Tuple slicing is used to produce a sub-tuple from a tuple.
It is performed using the slicing operator : (colon).
tuple_name(start:stop:step)
Tuple
Index: 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
| g | r | e | e | t | i | n | g |
+---+---+---+---+---+---+---+---+
Negative index: -8 -7 -6 -5 -4 -3 -2 -1
15
Tuple
Change items in a tuple:
Tuples are immutable (unchangeable).
Once a tuple is created, it cannot be modified.
31
Tuple
Add items in a tuple:
Tuples are immutable (unchangeable).
It is not allowed to add new items through indexing or function
such as insert(), append(), extend()
To add new items by merging tuples with the + operator will
create a new tuple.
32
16
Tuple
Remove items in a tuple:
Tuples cannot be modified, it means the items in a tuple cannot
be removed either.
33
Tuple
Tuple basic operations:
Python Expression Results Description
34
17
Tuple vs List
Comparison:
List Tuple
The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().
The list has the a variable length. The tuple has the fixed length.
The list provides more functionality than a tuple. The tuple provides less functionality than the list.
The list is used to store the simple collections with no The tuple is used to store the read-only collections i.e., the
constraints where the value of the items can be value of the items cannot be changed.
changed.
The lists are less memory efficient than a tuple. The tuples are more memory efficient because of its
immutability.
35
18