[go: up one dir, main page]

0% found this document useful (0 votes)
15 views27 pages

Chapter 5 Lists in Python

Chapter 5 discusses Python's list data type, which is an ordered, mutable, and heterogeneous collection of items. It covers list creation, indexing, slicing, and various operations such as concatenation, membership testing, and built-in functions for manipulating lists. The chapter also explains how to handle errors, compare lists, and utilize methods for adding, removing, and sorting elements.

Uploaded by

zararenolze
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)
15 views27 pages

Chapter 5 Lists in Python

Chapter 5 discusses Python's list data type, which is an ordered, mutable, and heterogeneous collection of items. It covers list creation, indexing, slicing, and various operations such as concatenation, membership testing, and built-in functions for manipulating lists. The chapter also explains how to handle errors, compare lists, and utilize methods for adding, removing, and sorting elements.

Uploaded by

zararenolze
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

9/17/2025

Chapter 5

 List is Python’s sequence data type.


 A list is an ordered collection of comma separated
values(items) within square brackets.
 Lists are heterogeneous, i.e., items in a list need not be
of the same type.
 Lists are mutable. i.e. values in the list can be modified.
 Values/ items that make up a list are known as
elements.
 No. of elements in a list is the length of the list.

1
9/17/2025

 Empty list is a list with no element.


Eg. L = [ ] or L = list()
 Long list is a list with many elements in it.
Eg: L = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
 Nested list is a list containing another list as its
element.
Eg: L = [0, 1, [2, 4, 6, 8], [3, 5, 7, 9]]

 Items in the list are stored according to its index.


 List indices can be positive or negative.
 Positive index 0 refers to the first element
 Negative index -1 refers to the last element
0 1 2 Positive index
 num = [ 1,2,3 ] 1 2 3
-3 -2 -1 Negative index

0 1 2 3
 my_list = [1, "Hello", 3.4, ‘$’] 1 Hello 3.4 $
-4 -3 -2 -1

2
9/17/2025

 A list is created by placing all the items/ expressions


(elements) inside square brackets [], separated by commas.
 Syntax :

List_name= [] or List_name= [value1, value2,……]

 This construct is called List Display construct.


 num = [ 1,2,3 ] # list of integers
 grade = [ ‘a’, ‘b’, ‘c’ ] # list of characters
 item = [ ‘one’, ‘two’, ‘three’ ] # list of strings
 my_list = [1, "Hello", 3.4] # list with mixed data types

1. list() method is used to convert a given tuple/record or


string into a list.
 Eg: new_list_name = list(sequence/ string)

3
9/17/2025

2. list() method is used to create an empty list.


new_list_name = list()
 Eg.:

3. list() method is used to create a list through user input


 Eg:

list5 is created with elements of list4 with


indices 3 to 6 (Index 7 is excluded)

list5 is created as a copy of list4

list5 will have all the elements of list4

4
9/17/2025

 In a list, items are represented using index/ subscript


value.
 Each item in a list has an index value.
 Starting index is always zero.
 Each element in the list is accessed through their index.
 Index can be :
 Positive (0, 1, 2, … forward) or
 Negative (-1, -2, … backward)

0 1 2 3 4
a e i o u
-5 -4 -3 -2 -1

#Index Error: list index out of range

If you give index outside the legal range, Python will raise
Index Error

5
9/17/2025

 We can change an item in a list with a particular index, by


assigning a new value to it.

 Accessing all elements of the list one after the other is called
Traversing a List.

 Len() returns the no. of elements in a list

6
9/17/2025

 Alias is a second name for a variable or


object.
 Both the names will refer to the same
memory location.
Temp
List
0 1 2 3 4
A B C D E

Divisions

 Two lists can be compared using the relational operators >,


<, >=, <=, ==, != and the result is given in terms of True/False

 While comparing, the corresponding elements of the two


lists must be of comparable types, otherwise python would
give an error. Example :

7
9/17/2025

 For non equality comparisons, like <, >, <=, >=, !=, python
ignores the corresponding elements that are equal and
compares only the corresponding elements which are different.
 Once a pair of different elements are compared, the result is
displayed based on that.

 In this example, elements in index 0, 1 and 2


are equal. So they are ignored. At index 3, the
elements are 40 and 35 . 40<35 is False. So
x1<x2 is False
 Next pair, 50 and 55, is not compared.

 Solved Questions : Q. 6

8
9/17/2025

List Operations Concatenation

Repetition

Membership Testing

Indexing

Slicing

 A process in which multiple lists can be combined together.


 Addition operator (+) is used as concatenation operator.
 Eg. 1.

 Eg. 2.

9
9/17/2025

 The * operator replicate a list specified no. of times.


 Eg. 1.

 Eg. 2.

 An operation used to check whether a particular element is a member of that


sequence or not.
 in operator returns true if element is a member of sequence otherwise it
returns false.
 not in operator returns false if element is a member of sequence otherwise it
returns true.
 Eg:

10
9/17/2025

 Index is a number specifying the position of an element


in a sequence.
 List indices can be positive or negative.
 Positive index 0 refers to the first element, 1 for second
element and n-1 for nth element
 Negative index -1 refers to the last element, -2 for
second last element and –n for the first element

L1
0 1 2 3 4
BLUE GREEN RED WHITE BLACK
-5 -4 -3 -2 -1

11
9/17/2025

 List can be divided into different ranges using slicing.


 List slices are the sub part of a list extracted out.
 The list slice is itself a list.
 Symbol :[colon] is used for slicing.
 Syntax: list[start : stop : step]

 start represents the starting index, stop represents


ending index and step represents the step size.
 step size of 1 picks every element, a step size of 2
means pick alternate elements, and so on.
 Default step value is 1.
 By default it starts from the first element(index 0) upto
the last element, if start and stop is not specified

12
9/17/2025

13
9/17/2025

# Method 1: Slice and store in another variable.


# Method 2: Using list()

# Method 3: Using copy() from the ‘copy’ library

 There are many built-in functions or methods in


Python to perform various operations on list.

14
9/17/2025

 Adds a single item to the end of a list

 Adds one list to the end of another list

15
9/17/2025

 Inserts an element at a specified index.


 This function takes 2 arguments:
1. Index number
2. Element to be added

 Reverses the items of the list.


 No new list is created

16
9/17/2025

 Returns the index of the first matched item


from the list

 Returns the length of the list.

17
9/17/2025

 Sorts the items of the list, by default in ascending/


increasing order.
 No new list is created.

 Removes all items from the list.


 List is emptied.
 No value is returned

18
9/17/2025

 Counts how many times an element has


occurred in the list and returns the number.

 Removes the element with the given index and also


returns it.
 If no index is specified, last element is removed.

19
9/17/2025

 Removes the specified element from the list.


 del with list slice is used when more than one
elements are to be removed.

 Used when we know the element to be deleted and


not it’s index.
 Removes only the first occurrence of that element

20
9/17/2025

 Returns the element in a list, with maximum value.


 To use max(), elements should be of the same type

 Returns the element in a list, with minimum value.


 To use min(), elements should be of the same type

21
9/17/2025

 Both are sequence data types


 Indexing - L[i] returns the item/character at index I of the
list/string (the first item has index 0)
 len() returns the length of a list/ string.
 Slicing - L[a:b] returns a new list/string, containing the
items/characters at indices between a and b(excluding b)
 Concatenation operator +, adds one list/ string to the end of
another.
 Replication operator *, repeats a list/ string specified no. of
times.

22
9/17/2025

 Q. 7
 Q. 8
 Q. 9
 Q. 10
 Q. 11

23
9/17/2025

24
9/17/2025

25
9/17/2025

26
9/17/2025

27

You might also like