[go: up one dir, main page]

0% found this document useful (0 votes)
676 views9 pages

List Manipulation

List is a mutable sequence that can hold elements of any data type. Lists can be indexed, sliced, and manipulated using built-in functions like append(), pop(), insert(), remove(), sort(), and reverse(). Common list operations include accessing elements, traversing lists, comparing lists, joining lists with +, replicating lists with *, and slicing lists.

Uploaded by

Harsh Bhola
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)
676 views9 pages

List Manipulation

List is a mutable sequence that can hold elements of any data type. Lists can be indexed, sliced, and manipulated using built-in functions like append(), pop(), insert(), remove(), sort(), and reverse(). Common list operations include accessing elements, traversing lists, comparing lists, joining lists with +, replicating lists with *, and slicing lists.

Uploaded by

Harsh Bhola
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/ 9

List Manipulation

 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 -
o [ ] Empty list
o [1, 2, 3] integers list
o [1, 2.5, 5.6, 9] numbers list (integer and float)
o [ ‘a’, ‘b’, ‘c’] characters list
o [‘a’, 1, ‘b’, 3.5, ‘zero’] mixed values list
o [‘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 –

• Long lists-

• Nested list -

Accessing a List
• First we will see the similarities between a List and a String:
o List is a sequence like a string.
o List also has index of each of its element.
o 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.
• Len() Function is used to get the length of list.

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


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

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.z
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 –
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.

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.
• To replicate a list, * operator , is used.

List Slicing
• To slice a List, syntax is:-

• Another syntax for List slicing is –


• Use of slicing for list Modification:-

List Functions and Methods


 Python provides some built-in functions for list manipulation.
 Syntax is like-
<list-object>.<method-name>

• List.index() function:

• List.append() function:
• List.extend() function:

• List.insert() function:

• List.pop() function:

• List.remove() function:

• List.clear() function:

• List.count() function:

• List.reverse() function:
• List.sort() function:

You might also like