Notes Chapter 7 List Manipulation

Introduction

• In Python, a list is a kind of container that contains collection of any kind of values.

• A List is a mutable data type which means any value from the list can be changed. For changed values , Python does not create a new list.

• List is a sequence like a string and a tuple except that list is mutable whereas string and tuple are immutable.

• In this chapter we will see the manipulation on lists. We will see creation of list and various operation on lists via built in functions.

List Creation

• List is a standard data type of Python. It is a sequence which can store values of any kind.

• List is represented by square brackets “ [ ] “
 For ex –
[ ]                                Empty list
[1, 2, 3]                        integers list
[1, 2.5, 5.6, 9]               numbers list (integer and float)
[ ‘a’, ‘b’, ‘c’]                   characters list
[‘a’, 1, ‘b’, 3.5, ‘zero’]     mixed values list
[‘one’, ’two’, ’three’]       string list

• In Python, only list and dictionary are mutable data types, rest of all the data types are immutable data types.

Creation of List

•List can be created in following ways-

•Empty list –
L = [ ]

•list can also be created with the following statement-
L = list( )

Notes Chapter 7 List Manipulation

•Long lists-
even = [0, 2, 4, 6, 8, 10 ,12 ,14 ,16 ,18 ,20 ]

•Nested list –
L = [ 3, 4, [ 5, 6 ], 7]

Notes Chapter 7 List Manipulation

-As we have seen in the example That when we have supplied values as numbers to a list even then They have automatically converted to string

Notes Chapter 7 List Manipulation

–If we want to pass values to a list in numeric form then we have to write following function –
eval(input())
L=eval(input(“Enter list to be added “))

Notes Chapter 7 List Manipulation

Accessing a List

• First we will see the similarities between a List and a String.

• List is a sequence like a string.

• List also has index of each of its element.

• Like string, list also has 2 index, one for forward indexing (from 0, 1, 2, 3, ….to n-1) and one for backward indexing(from -n to -1).

• In a list, values can be accessed like string.

Notes Chapter 7 List Manipulation

•len( ) function is used to get the length of a list.

Notes Chapter 7 List Manipulation

•L[ i ] will return the values exists at i index.

•L [ i : j ] will return a new list with the values from i index to j index excluding j index.

Notes Chapter 7 List Manipulation

Difference between a List and a String

• Main difference between a List and a string is that string is immutable whereas list is mutable.

• Individual values in string can’t be change whereas it is possible with list.

Notes Chapter 7 List Manipulation

Traversal of a list

•Traversal of a list means to access and process each and every element of that list.

•Traversal of a list is very simple with for loop –

Notes Chapter 7 List Manipulation

Comparison of Lists

•Relational operators are used to compare two different lists.

•Python compares lists or tuples in lexicographical order, means comparing sequences should be of same type and their elements should also be of similar type.

Notes Chapter 7 List Manipulation

List Operations (+, *)

• Main operations that can be performed on lists are joining list, replicating list and list slicing.

• To join Lists,+ operator , is used which joins a list at the end of other list. With + operator, both the operands should be of list type otherwise error will be generated.

Notes Chapter 7 List Manipulation

• To replicate a list, * operator , is used.

Notes Chapter 7 List Manipulation

List Slicing

• To slice a List, syntax is

Notes Chapter 7 List Manipulation

• Another syntax for List slicing is –

Notes Chapter 7 List Manipulation

Use of slicing for list Modification

Look carefully at following examples-

Notes Chapter 7 List Manipulation

List Manipulation

• Element Appending in List

Notes Chapter 7 List Manipulation

• Updating List elements

Notes Chapter 7 List Manipulation

• Deletion of List elements

Notes Chapter 7 List Manipulation

List Manipulation

• Only one element will be deleted on pop() from list.

• pop ( ) function can not delete a slice.

• pop ( ) function also returns the value being deleted.

Notes Chapter 7 List Manipulation

List Functions and Methods

–Python provides some built-in functions for list manipulation

–Syntax is like

Notes Chapter 7 List Manipulation

List Functions and Methods

List.index ( ) function:

Notes Chapter 7 List Manipulation

List.append( ) function:

Notes Chapter 7 List Manipulation

List.extend( ) function:

Notes Chapter 7 List Manipulation

List.insert( ) function:

Notes Chapter 7 List Manipulation

List.pop ( ) function:

Notes Chapter 7 List Manipulation

List.remove( ) function:

Notes Chapter 7 List Manipulation

List.count( ) function:

Notes Chapter 7 List Manipulation

List.reverse( ) function:

Notes Chapter 7 List Manipulation

List.sort( ) function:

Notes Chapter 7 List Manipulation

Important

• To sort a list in reverse order, write in following manner–
                         List.sort(reverse=True)

• If a list contains a complex number, sort will not work.

Notes Chapter 7 List Manipulation

Related Posts

error: Content is protected !!