0% found this document useful (0 votes)
19 views35 pages

LISTS PDF RSV

Uploaded by

sunaina bokolia
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)
19 views35 pages

LISTS PDF RSV

Uploaded by

sunaina bokolia
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/ 35

Lists

 A list is a data-structure, or it can be considered a container that


can be used to store multiple data at once.

 Like a String, List also is, sequence data .

 In a string we have only characters but a list consists of data of


multiple data types

 It is an ordered set of values enclosed in square brackets [].

 We can use index in square brackets []

 Values in the list are called elements or items.

 A list is mutable type i.e we can change value in place without


creating a fresh list.
 List is a type of sequence like tuples and string but is different
from tuple and string in the way that lists are mutable but
string and tuples are immutable.

 The elements are indexed according to a sequence and the


indexing is done with 0 as the first index

 List index works the same way as String index :

 An integer value/expression can be used as index

 An Index Error appears, if you try and access element that does
not exist in the list
IndexError: list index out of range

 An index can have a negative value, in that case counting


happens from the end of the list.
Creating List
 To create a list, you separate the elements with a comma and enclose
them with a bracket “[]”.

• The syntax is: <name of list> = [ <value>, <value>, <value> ]


• For example:
 >>> list1 = [67, 82, 98, 92, 78, 87]; # a numeric list

 >>> list2 = ['Pen', 'Pencil', 'Rubber', 'Scale', 'Eraser'] # a character/string list

 >>> list3 = ['A V Raman', 35, 'TGT Computer', 470.00] # a list with multiple type values

 >>> list4 = [] # an empty list


CREATING LIST WITH list( ) METHOD
CREATING AN EMPTY LIST
CREATING LIST WITH SEQUENCE
• We can use the following syntax:
• ListName=list(sequence)
With String

With Tuple

We can also create a list of single or single digits through keyboard input:

To store list through input in Python we can use eval() to convert the list items.:
Accessing/Printing List Elements
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
For example:
elements
>>> print (list1) # prints in82,a98,list
all elements [67, can
92, 78, 87] be accessed by
specifying the list name followed by a number in
>>> print (list1[4]) # prints 5th element 78
square brackets ( [ ] ).
>>> list1[-2] # prints 2nd last element 78
Example of list:
Slicing List Elements
 Selecting a slice is similar to selecting an element(s) of a list.
 Subsets of lists can be taken using the slice operator with two indices in square brackets
separated by a colon ( [m:n] ).
 The operator [m:n] returns the part of the list from the mth position and up to but not
including position n, i.e., n – 1.
List Slices
Examples
>>> L=[10,20,30,40,50]
>>> print(L[1:4])
[20, 30, 40] #print elements 1st index to 3rd index

>>> print(L[3:])
[40, 50] #print elements from 3rd index onwards

>>> print(L[:3])
[10, 20, 30] #print elements 0th index to 2nd index

>>> print L[0:5:2]


[10, 30, 50] #print elements 0th to 4th index jump 2 steps
Concatenating & Replicating List
LIST CONCATENATION
LIST SLICING

COPYING ALTERNATIVE
ELEMENTS
OF LIST BY SLICING

CREATING MULTIPLE LISTS –


LIST MAPPING
Lists are Mutable
Lists are mutable, which means we can change their elements. Using the bracket operator ([ ])
on the left side of an assignment, we can update one of the elements. For 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']
Using Membership Operators with List
Comparing list
Python allow us to use all the relational
operators on list like == ,>=, <=,!=, > ,<
Python will compare individual elements of each
list.

NON-EQUALITY COMPARISION IN LIST SEQUENCE


Traversing a list
Traversing a List
• Using while loop
L=[1,2,3,4]
i=0
while i < 4:
print (L[i])
i+=1

Output
1234
Traversing a List
Using for loop

L=[1,2,3,4,5] L1=[1,2,3,4,5]
for i in L: for i in range (5):
print(i) print(L1[i])

Output: Output:
12345 12345
READING LIST THROUGH KEYBOARD
USE OF append ( ) Method
• append () method is used to add one element to the end
of list. Every time new element is assigned using append
it will be added at the rear side of list
• Syntax:
• Listobject.append(element)
• Example
• L1.append(5) OR
• element=int(input())
• L1.append(element)
Deleting Elements of LIST

• It is possible to delete/remove element(s) from the list.


There are many ways of doing so:
• (i) If index is known, we can use pop ( ) or del
• (ii) If the element is known, not the index, remove ( ) can be
used.
• (iii) To remove more than one element, del ( ) with list slice
can be used.
• (iv) Using assignment operator
• 1. pop ( ) Method
pop() removes the element from the specified index, and also return the element which
was removed.
Its syntax is:
List.pop ([index])

2. del Method
del removes the specified element from the list, but does not return the deleted value.
Syntax :
del Listobject[index]

3. remove ( ) Method
In case, we know the element to be deleted not the index, of the element, then remove ()
can be used.
Syntax is:
listobject.remove(value)

>>>L1.remove(60)
Examples
>>> l=[10,20,30,40,50]
>>> l.pop() #last index elements popped
50
>>> l.pop(1) # using list index
20
>>> l.remove(10) #using element
>>> print(l)
[30, 40]

>>> l=[1,2,3,4,5,6,7,8]
>>> del l[2:5] #using range
>>> print(l)
[1, 2, 6, 7, 8]
1. insert () Method
 insert () method allows us to insert an element, at the given position specified
by its index, and the remaining elements are shifted to accommodate the new
element.
 insert () requires two arguments-index value and item value.
Its syntax is
list. insert (index, item)

2. reverse ( ) Method
reverse() method can be used to reverse the elements of the list in place
Its syntax is:
list.reverse ( )
Method does not return anything as the reversed list is stored in the same variable
3. sort ( ) Method
For arranging elements in an order Python provides a method sort ( ) and a function
sorted ( ).
sort ( ) modifies the list in place and sorted ( ) returns a new sorted list.
Default Ascending
Order sorting

Descending order

Sorting using key value (length of string)


4. clear ( ) Method
clear() method deletes or clears the content of list. For Example:

5. index ( ) Method
index() method is used to know the index of an element in a given list

6. extend ( ) Method
extend() method is used for Extending a given list with another list.
Difference between append() and extend()
• append() allows to add only 1 item to a list, extend() can add multiple items to a list
Linear search program

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy