Professional Documents
Culture Documents
FALLSEM2022-23 BCSE101E ETH VL2022230106689 Reference Material I 03-11-2022 List
FALLSEM2022-23 BCSE101E ETH VL2022230106689 Reference Material I 03-11-2022 List
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’]
List Length
• thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(len(thislist))
Accessing elements
indexing
• print(thislist[1])
• Negative indexing
• print(thislist[-1])
• Range of indexes
• print(thislist[2:5])
• print(thislist[:4])
• print(thislist[2:])
• print(thislist[-4:-1])
• Change item value
• thislist[1] = "blackcurrant“
• thislist =
["apple", "banana", "cherry", "orange", "kiwi", "man
go"]
thislist[1:3] = ["blackcurrant", "watermelon"]
• Insert Items
Without replacing any of the existing values
insert(index,value)
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
Append items
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
Remove list items
• thislist.remove("banana")
• thislist.pop(1)
• thislist.pop()
• del thislist[0]
• del thislist
• thislist.clear()
• Sorting
• thislist.sort()
• thislist =
["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
• thislist.reverse()
Basic Operations in List
• list2 = list1
• thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
• thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
Join Lists
• list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
• list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
• list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
List Comprehensions
• fruits =
["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
• fruits =
["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
newlist =
[expression for item in iterable if condition == Tru
e]
newlist = [x for x in fruits if x != "apple"]
newlist = [x for x in fruits]
newlist = [x for x in range(10) if x < 5]
newlist = [x.upper() for x in fruits]
Methods
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value