0% found this document useful (0 votes)
49 views33 pages

List in Python

It's a Presentation for class 11th CS students... list in python.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views33 pages

List in Python

It's a Presentation for class 11th CS students... list in python.

Uploaded by

Harshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

LISTS IN

PYTHON
By Harshita Gupta
WHAT IS LIST?
 List is mutable sequence of values seperated by commas, enclosed in square
brackets.
 Elements of lists consists of different data types, i.e., strings, int, float, and even
another list can be the element of the main list.
 e.g:- list1=[].

Here the list1 is the example of Empty list, as it’s name suggest, this type of list
doesn’t have any Element in it.
 Some examples regarding list having heterogeneous data type consists in it:-

List1=[10,20,30.5, ‘Red’, ‘blue’, [ 200, ‘Computer’, ‘OOPS’]]


CREATING/INITIALISING LIST
 List can be initialised by putting comma-seprated values within square
brackets, square brackets indicate start and end of the list.
e.g:- list1=[]
print(list1)
Output:-
[]
LISTS TYPES
 Lists in python broad classification into three types:-
 Empty lists:- Empty lists don’t have any element, like:

e.g:- list1=[]
Output:- []
Nested lists:- A nested list have list inside a list, or in other words, A list
containing another list as element
e.g:- mylist=[10,20,30,[‘my’, ‘love’, ‘apple’], ‘mango’]
print(mylist)
Output:- [10,20,30,[‘my’, ‘love’, ‘apple’], ‘mango’]
Long lists:- As for the long list, they contain many elements, which make a list
long,...in other words long lists consists of many elements of same or different data
types.
e.g:- mylist=[20,30,30,[‘my’, ‘love’, ‘apple’], ‘mango’, ‘lol’, ‘rocket’]
output:- [10,20,30,[‘my’, ‘love’, ‘apple’], ‘mango’, ‘lol’, ‘rocket’]
CREATING LIST FROM A
SEQUENCE
my_list=list(‘Computer’)
print(my_list)
Output:-
[‘C’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
CREATING LIST BY USER
INPUTS METHOD
 list1=list(input(‘Enter the values:’))

print(list1)
Enter the values: Hi Python
Output:-
[‘H’, ‘i’, ‘’, ‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
ACCESSING LIST ELEMENTS
 A list consists of any collection of items of items which are stored according
to its index.
 We can access any desired value by it’s indexing value.
 Indexing value are of two types:-

1. Positive indexing starts from left to right (0 to(n-1))


2. Negative indexing starts from right to left (-1 to-n)
COMPARING LISTS
 Python let us compare two lists .Each element is individually compared
in lexicographical (alphabetical and dictionary) order. All the relational
operators (>,<,==,!=) can be applied. They can be compared of they are
of comparable types
OPERATIONS ON LISTS
 Concatenation
 Repetition
 Membership testing
 Indexing
 Slicing
CONCATENATION
 Concatenation or Joining is a process to combine or join multiple lists
together with the help of certain operators.
 Python allows us join two list using ‘+’ concatenation operator.
REPETITION/REPLICATION
 Multiply (*asterisk) operator replicates the list for a specified number of
times and creates a new list.
e.g:- list5=[10,20,30]
list5*2
Output:-
[10,20,30,10,20,30]
MEMBERSHIP TESTING
 Membership operator helps us to check whether a element is a member of
that sequence or not.
 We can use ‘in’ or ‘not in’ operator in any list
IN OPERATOR
INDEXING
 Indexing is a particular place alloted for a particular element, we can get
any element just by it’s indexing number.
 e.g:- list=[10, 20, 30, ‘love’, ‘logical’]

print(list[3])
print(list[-1])
Output:-
love
logical
SLICING
 Indexing and slicing are two inter-related operations in lists. Slicing is done
through indexing.
 Slicing is an operation in which we can slice a particular range from that
sequence.
To select a specific elements from the list
BUILT-IN FUNCTIONS
 Append:- list.append(elem)
 Insert:- list.insert(index,elem)
 Extend:- list.extend(list2)
 Index:- list.index(elem)
 Remove:- list.remove(elem)
 Sort:-list.sort()
 Reverse:- list.reverse()
APPEND()
 The Append() method adds a single item to the end of the list.
 It doesn’t create a new list, it just modifies the original list.

Syntax:
list.append(item)
EXTEND()
 This method adds one list at the end of another list.
 In implying, all the items of a list are added to an already created list.

Syntax:
list.extend(list2)
INSERT()
 The insert() can be used to insert an element at a specific index.
 Syntax: list_name.insert(insert_number, value)
REVERSE()
 The reverse() reverses the order of the elements in a list._
 It doesn’t create any new list.
INDEX()
 Index() returns the index of first matched item from the list.
 It returns the first occurrence of an item for which the index position is to
be searched for in the list.
UPDATING LIST
 Lists are mutable, new value can be assigned to existing value. Using phone‘=‘.

List[index]=<new value>
LEN()
 Len() function returns length of the list , i.e., number of elements in a list.
SORT()
 This sorts the items of the list, by default in increasing order.
 It doesn’t create a new list.
CLEAR()
 Clear() method removes all items from the list.
 Clear() method just empties the given list, and doesn’t return any value.
DELETION OPERATION
 Methods to delete item from a list.
 If index is known, pop() or del statement.
 If element is known but not the index, remove() is used.
 To remove more than one element, del() with list slice can be used.
POP()
 It removes the elements from a specified index and also returns the
element which was removed.
Syntax:
List.pop(index)
DEL STATEMENT
 Del statement removes the specified element from the list but does not
return the deleted value.
REMOVE()
 The remove() function is used when the value to be deleted is known but
not the index of the element.
THANK YOU

You might also like