[go: up one dir, main page]

0% found this document useful (0 votes)
12 views18 pages

Lec 03 - List and Tuple

Uploaded by

Abc
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)
12 views18 pages

Lec 03 - List and Tuple

Uploaded by

Abc
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/ 18

Chapter 3

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.

 The important properties of lists are as follows:


 The lists are ordered.
 lists that have the same elements in a different order are not the same.
 The element of the list can be accessed by index.
 list index starts at 0
 Lists can contain any sort of data type.
 It can be numbers, strings, tuples and even other lists.
 List is mutable type.
 the element can be changed once it is created. 4

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.

 List slicing returns a new list from the existing list.

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

try to display the


non-existing list

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

len(['a', 'b', 'c']) 3 Length

[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation

['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition

2 in [2, 3, 4] True Membership

for x in [5, 6, 7] : 567 Iteration


print (x, end = ' ')

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

 The parentheses are optional but it is good practice to use.


 T2 = 1, 'MMU', 7.82, True # tuple without parentheses
 T3 = ("Melaka", "Johor", "Pahang", "Selangor") #same data type
 T4 = ('a', 'b', 4.6, 8.9, 50) #mixed data type
 T5 = (('ST', 'DCN’), (10, 20, 30, 40)) #nested 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)

start: the beginning index of the slice.


stop: the ending index of the slice (not include the ending index).
step: the amount by which the index increases, defaults to 1.

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.

 However, tuple can be removed entirely using the keyword del.

33

Tuple
 Tuple basic operations:
Python Expression Results Description

len(('a', 'b', 'c')) 3 Length

(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Concatenation

('Hi!’)* 4 Hi!Hi!Hi!Hi! Repetition

2 in (2, 3, 4) True Membership

for x in (5, 6, 7) : 567 Iteration


print (x, end = ' ')

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 is mutable. The tuple is immutable.

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

You might also like