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

Lists in Python

The document discusses lists in Python. It defines lists as ordered and mutable containers that can store elements of different data types. It describes how to create, access, modify, compare and perform operations on lists using built-in functions and methods like append(), pop(), sort(), etc. Lists allow indexing, slicing, traversal and support operators like + for concatenation and * for repetition. Being mutable, lists can be modified after their creation by adding, removing or updating elements.

Uploaded by

Nehmat Kaur
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)
72 views

Lists in Python

The document discusses lists in Python. It defines lists as ordered and mutable containers that can store elements of different data types. It describes how to create, access, modify, compare and perform operations on lists using built-in functions and methods like append(), pop(), sort(), etc. Lists allow indexing, slicing, traversal and support operators like + for concatenation and * for repetition. Being mutable, lists can be modified after their creation by adding, removing or updating elements.

Uploaded by

Nehmat Kaur
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/ 28

Lists in Python

Introduction
 In Python, list is a type of container, which is used to store
multiple data at the same time.
 The list in Python are ordered and have a definite count.
 The elements in a list are indexed according to a definite
sequence and the indexing of a list is done with 0 being the
first index.
 Lists are mutable, and hence, they can be altered even after
their creation.
 List doesn’t need a built-in function for creation of list. A list
may contain duplicate values
 A list is also a dynamic and mutable type, i.e., we can add
and delete elements from the list at any time.
 By comparison an array is an ordered collection of items of a
single type but in principle a list is more flexible than an
array and can hold multiple type values.
 The values that make up a list are called its elements. Each
list value is a single element in the list. The elements are
indexed according to a sequence and the indexing is done
with 0 as the first index.
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( )

• Long lists-
This is tuple
even = [0, 2, 4, 6, 8, 10 ,12 ,14 ,16 ,18 ,20 ]
• Nested list -
L = [ 3, 4, [ 5, 6 ], 7]
Another method
Creation of List
-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
– 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 “))

eval ( ) function identifies type of the passed string and then return it.

Another example

String Values
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.
Forward index 0 1 2 3 4 5 6 7 8 9 10 11 12 13
List R E S P O N S I B I L I T Y
Backward index -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
 In order to access the list items refer to the index
number. The index must be an integer.
 A list index starts from 0.
 We can access individual list elements using indexing
and a range of elements using slicing. A list index starts
from 0.
 Python indexes the list element from left to right and from
right end to left. From left to right, the first element of a list
has the index 0 and from right end to left, the extreme right
index element of a list is –1. Individual elements in a list
can be accessed by specifying the list name followed by a
number in square brackets ( [ ] ).
Accessing a List
• len( ) function is used to get the length of a list.
Important 1:membership
operator (in, not in) works
in list similarly as they work
in other sequence.

• 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.
Important 2: + operator
adds a list at the end of
other list whereas *
operator repeats a list.
Lists are Mutable
example:
>>> friends = ['Anmol', 'Kiran', 'Vimal', 'Sidharth', 'Riya'] # a list with five friends
>>> friends[0] = 'Jatin' # lists 0th position value changed with new name Jatin
>>> friends[-1] = 'Vidya' # lists last position value changed with new name Vidya
>>> print (friends)
['Jatin', 'Kiran', 'Vimal', 'Sidharth', 'Vidya']
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.
Value didn’t
change in string.
Error shown. Value got changed
in list specifying
list is mutable
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 –
for <item> in <list>:
*Python supports UNICODE therefore
output in Hindi is also possible
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.

Some
examples

• In first example, python did not


raise the error because both the
lists are same.
• In second comparison, both the
lists are not similar hence, python
raised the error.
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 seq = list [ start : stop ]

• Another syntax for List slicing is –


seq=list[start:stop:step]
Use of slicing for list Modification
• Look carefully at following examples-

New value is being assigned here.

Here also, new value is being assigned.

See the difference between both the results.

144 is a value and not a sequence.


List Manipulation
• Element Appending in List list.append(item)

• Updating List elements list[index]=<new value>

• Deletion of List elements del list[index]


Important: del
command can be
used to delete an Important: if we write del list complete
element of the list list will be deleted.
or a complete slice
or a complete list.
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.

Last item

6th item

1st item
List Functions and Methods
– Python provides some built-in functions for list manipulation
– Syntax is like <list-object>.<method-name>
Function Details

List.index(<item>) Returns the index of passed items.

List.append(<item>) Adds the passed item at the end of list.

List.extend(<list>) Append the list (passed in the form of argument) at the end of list
with which function is called.

List.insert(<pos>,<item>) Insert the passed element at the passed position.

List.pop(<index>) Delete and return the element of passed index. Index passing is
optional, if not passed, element from last will be deleted.

List.remove(<value>) It will delete the first occurrence of passed value but does not
return the deleted value.
List Functions and Methods
Function Details

List.clear ( ) It will delete all values of list and gives an empty list.

List.count (<item>) It will count and return number of occurrences of the passed element.

List.reverse ( ) It will reverse the list and it does not create a new list.

List.sort ( ) It will sort the list in ascending order. To sort the list in descending
order, we need to write----- list.sort(reverse =True).
List Functions and Methods
• List.index ( ) function:

• List.append( ) function:

• List.extend( ) function:

• List.insert( ) function:
List Functions and Methods
• List.pop ( ) function:

• List.remove( ) function:

• List.count( ) function:
List Functions and Methods
• List.reverse( ) function:

• List.sort( ) function:

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

You might also like