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

Week 6 - List Updated

The document discusses Python lists including creating and accessing lists, adding and removing elements from lists, slicing lists, list comprehensions, and list methods. Lists are introduced as a mutable sequence that can contain heterogeneous data types and their elements can be accessed by index.

Uploaded by

Rani Muniandy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Week 6 - List Updated

The document discusses Python lists including creating and accessing lists, adding and removing elements from lists, slicing lists, list comprehensions, and list methods. Lists are introduced as a mutable sequence that can contain heterogeneous data types and their elements can be accessed by index.

Uploaded by

Rani Muniandy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

• Chapter 7

List in Python
Week 6

7-1
Topics (1 of 2)
• Sequences
• Introduction to Lists
• List Slicing
• Finding Items in Lists with the in Operator
• List Methods and Useful Built-in Functions

7-2
Topics (2 of 2)
• Copying Lists
• Processing Lists
• List Comprehensions
• Two-Dimensional Lists
• Tuples
• Plotting List Data with the matplotlib Package

7-3
Sequences
• Sequence: an object that contains multiple items of
data
– The items are stored in sequence one after another
• Python provides different types of sequences,
including lists and tuples
– The difference between these is that a list is mutable
and a tuple is immutable

7-4
Python List
• Python Lists are just like dynamically sized arrays,
declared in other languages (vector in C++ and ArrayList in
Java).
• In simple language, a Python list is a collection of thing
• Lists are the simplest containers that are an integral part of
the Python language.
• Lists need not be homogeneous always which makes it the
most powerful tool in Python.
• A single list may contain DataTypes like Integers, Strings,
as well as Objects.
• Lists are mutable, and hence, they can be altered even
after their creation.s, enclosed in [ ] and separated by
commas. 7-5
List – Example
• Lists in Python can be created by just placing the
sequence inside the square brackets[].
• a list doesn’t need a built-in function for its creation of
a list.
• Example 1 – Creating List in Python

7-6
Example 1 – List in Python

7-7
Example 2: Creating a list with multiple
distinct or duplicate elements
• A list may contain duplicate values with their distinct
positions and hence, multiple distinct or duplicate
values can be passed as a sequence at the time of list
creation.

7-8
Accessing elements from the List

• In order to access the list items refer to the index


number.
• Use the index operator [ ] to access an item in a list.
The index must be an integer.
• Nested lists are accessed using nested indexing.

7-9
Example 3: Accessing elements from
list

7 - 10
Example 4: Accessing elements from a
multi-dimensional list

7 - 11
Negative Indexing
• In Python, negative sequence indexes represent
positions from the end of the array.
• Instead of having to compute the offset as in
List[len(List)-3], it is enough to just write List[-3].
• Negative indexing means beginning from the end, -1
refers to the last item, -2 refers to the second-last
item, etc.

7 - 12
Example 5 -Assessing – Index List

7 - 13
Getting the Size of List
• Python len() is used to get the length of the list.

7 - 14
Taking Input of a Python List

• We can take the input of a list of elements as string,


integer, float, etc.
• But the default one is a string.

7 - 15
Example 8

7 - 16
Explanation

7 - 17
Adding Elements to a Python Lis

• Method 1: Using append() method


• Method 2: Using insert() method
• Method 3: Using extend() method

7 - 18
Method 1: Using append() method

• Elements can be added to the List by using the built-


in append() function.
• Only one element at a time can be added to the list by
using the append() method, for the addition of multiple
elements with the append() method, loops are used.
• Tuples can also be added to the list with the use of
the append method because tuples are immutable.
• Lists can also be added to the existing list with the use
of the append() method.

7 - 19
Example 9

7 - 20
Method 2: Using insert() method
• append() method only works for the addition of
elements at the end of the List.
• for the addition of elements at the desired position,
insert() method is used.
• Unlike append() which takes only one argument, the
insert() method requires two arguments(position,
value).

7 - 21
Example 10

7 - 22
Method 3: Using extend() method
• Other than append() and insert() methods, there’s one
more method for the Addition of elements, extend().
• this method is used to add multiple elements at the
same time at the end of the list.
• Note: append() and extend() methods can only add
elements at the end.

7 - 23
Example 11

7 - 24
Reversing a List
A list can be reversed by using the
reverse() method in Python.

7 - 25
Removing Elements from the List
Using remove() method
• Elements can be removed from the List by using the
built-in remove() function but an Error arises if the
element doesn’t exist in the list.
• Remove() method only removes one element at a
time, to remove a range of elements, the iterator is
used.
• The remove() method removes the specified item.

7 - 26
Example 13

7 - 27
Slicing a list
• We can get substrings and sublists using a slice. In
Python List, there are multiple ways to print the whole
list with all the elements,
• but to print a specific range of elements from the list,
we use the Slice operation.
• Slice operation is performed on Lists with the use of a
colon(:).

7 - 28
Example 14 – Slicing

7 - 29
Example 15 – Slicing Index List

7 - 30
List Comprehension
• Python List comprehensions are used for creating
new lists from other iterables like tuples, strings,
arrays, lists, etc.
• A list comprehension consists of brackets containing
the expression, which is executed for each element
along with the for loop to iterate over each element.

7 - 31
Example 16 – List Comprehension

7 - 32
Example 16 – Explanation

7 - 33
Methods in List

7 - 34
Summary

7 - 35

You might also like