Lists
Lists
Examples
• Apple, Banana, Berry, Mango
• Football, Basketball, Throwball, Tennis, Hockey
• Sunrise, Sugar, Cheese, Butter, Pickle, Soap,
Washing Powder, Oil….
• Agra, Delhi, Kashmir, Jaipur, Kolkata…
Introduction
• Contains multiple values that are logically related
• List is a type of mutable sequence in Python
• Each element of a list is assigned a number – index
/ position
• Can do indexing, slicing, adding, multiplying, and
checking for membership
• Built-in functions for finding length of a sequence
and for finding its largest and smallest elements
What is a List?
• Most versatile data type in Python
• Comma-separated items can be collected in
square brackets
• Good thing is..
– THE ITEMS IN THE LIST NEED NOT BE OF SAME
TYPE
Creating a list
• Creating an EMPTY • Creating a list with items
list listname = [item1, item2, ….]
listname = [] Example:
Example: Temp = [100, 99.8, 103, 102]
L1 = [] S = [‘15BIT0001’, ‘Achu’, 99.9]
MyList = []
L2 = [1, 2, 3, 4, 5, 6, 7]
Books = []
Course = [‘Python’, ‘C’, ‘C++’,
‘Java’]
Accessing Values
• Using index or indices
>>>L1 = [1, 2, 3, 4, 5, 6]
>>>print (L1[3]) #indexing
>>>4
>>>print (L1[2:5]) #slicing
>>>[3, 4, 5]
Updating Elements
• Update an element in list using index
>>>L1 = [1, 2, 3, 4, 5, 6]
>>>L1[2] = 111
>>>L1
[1, 2, 111, 4, 5, 6]
Deleting Elements
• Delete an element in list using index
>>>L1 = [1, 2, 111, 4, 5, 6]
>>>del L1[4]
>>>L1
[1, 2, 111, 4, 6]
Basic Operations in List
• >>> len([1, 2, 3]) # Length
3
• >>> [1, 2, 3] + [4, 5, 6] # Concatenation
[1, 2, 3, 4, 5, 6]
‘Delhi'
>>> L[−2] # Negative: count from the right
‘Bombay'
>>> L[1:] # Slicing fetches sections
[‘Bombay', ‘Delhi']
Insertion, Deletion and Replacement
>>> L = [1, 2, 3]
>>> L[1:2] = [4, 5] # Replacement/insertion
>>> L
[1, 4, 5, 3]
>>> L[1:1] = [6, 7] # Insertion (replace nothing)
>>> L
[1, 6, 7, 4, 5, 3]
>>> L[1:2] = [] # Deletion (insert nothing)
>>> L
[1, 7, 4, 5, 3]
Insertion, Deletion and Replacement
>>> L = [1]
>>> L[:0] = [2, 3, 4]
# Insert all at :0, an empty slice at front
>>> L
[2, 3, 4, 1]
>>> L[len(L):] = [5, 6, 7]
# Insert all at len(L):, an empty slice at end
>>> L
[2, 3, 4, 1, 5, 6, 7]
List method calls
>>> L = ['eat', 'more', 'SPAM!']
>>> L.append('please')
>>> L
>>> L [1, 2, 3, 4, 5]
>>> L.pop()
5
Other common list methods
>>> L [1, 2, 3, 4]
>>> list(reversed(L))
# Reversal built-in with a result (iterator)
[1, 2, 3, 4]
Other common list methods
>>> L = ['spam', 'eggs', 'ham']
>>> L.index('eggs') # Index of an object (search/find)
1
>>> L.insert(1, 'toast') # Insert at position
>>> L
['spam', 'toast', 'eggs', 'ham']
>>> L.remove('eggs') # Delete by value
>>> L
['spam', 'toast', 'ham']
Other common list methods
>>> L.pop(1) # Delete by position 'toast'
>>> L
['spam', 'ham']
>>> L.count('spam') # Number of occurrences 1
1
Other common list methods
matrix3=matrix1+matrix2
print(matrix3)
print(len(matrix1))
for i in range(0,len(matrix1)):
for j in range(0,len(matrix1)):
matrix3[i][j]=matrix1[i][j]+matrix2[i][j]
print(matrix3)
Matrix Addition
print(matrix)
for i in range(0,len(matrix1)):
for j in range(0,len(matrix1)):
matrix[i][j]=matrix1[i][j]+matrix2[i][j]
print(matrix)
print(matrix3)
Generic 2D List
a = []
for i in range(3):
a.append([])
for j in range(3):
a[i].append(i+j)
print(a)
Strings and Lists
>>> S = 'spammy'
>>> L = list(S)
>>> L
['s', 'p', 'a', 'm', 'm', 'y']
>>> L[3] = 'x' # Works for lists, not strings
>>> L[4] = 'x‘
>>> L
['s', 'p', 'a', 'x', 'x', 'y']
>>> S = “ ”.join(L) #uses ‘’ for joining elements of list
>>> S
'spaxxy‘
Strings and Lists
>>> 'SPAM'.join(['eggs', 'sausage', 'ham', 'toast'])
'eggsSPAMsausageSPAMhamSPAMtoast‘
# uses ‘SPAM’ for joining elements of list
>>> line = 'aaa bbb ccc'
>>> cols = line.split()
>>> cols
['aaa', 'bbb', 'ccc']
Reading Values for a List
n=int(input())
l2=[]
for i in range(0,n):
points = int(input())
l2.append(points)
print(l2)
1. In Caesar cipher, each letter is replaced by another letter which
occurs at the d-th position (when counted from the position of the
original letter), in the English alphabet. For identifying the
position of a letter, we follow the usual order of the English
alphabet, from a to z. Given a word and a positive integer d, use
Caesar cipher to encrypt it. For example, if the word is 'ball' and
the value of 'd' is 3 then the new encrypted word is 'edoo'. 'x' will
be replaced by 'a', 'y' should be replaced by 'b' and 'z' should be
replaced by 'c'.
<char><char><char><char><char><digit><digit><digit><digit><char>
Your task is to figure out if the PAN number is valid or not. A valid PAN
number will have all its letters in uppercase and digits in the same
order as listed above.
1. Given the marks of ten students 10,20,25,34,11,33,44,67,79,81.
Write a program to identify the odd and even numbers and place
it in separate lists.
11 2 4
4 5 6
10 8 -12