List
List
• Accessing String
• String Slicing
• Accessing List
• List Slicing
• List Cloning/Copy
Python Lists
A list is a sequence of data values called items or elements. The items in
a list need not be of the same type.
Creating a List
The list is defined as a list of comma-separated values (items) in square
brackets.
Negative index starts from the right side of the list and ranges
from -1 to minus length of the list (i.e. –length ).
Example
List2= [[1,2], 4, 5]
#Accessing Elements
-List2[0], List2[1], List2[2], List2[0][0], List2[0][1]
Creating List Using List Function
The range function returns a list of numbers that range from zero to
one less than the parameter.
Example:
list( range(4) ) ------[0, 1, 2, 3]
list( range(1, 4) ) ------[1, 2, 3]
list( range(2, 10, 2) ) ------[2, 4, 6, 8]
list( range(-1, -11, -2) ) ------[-1, -3, -5, -7, -9]
Lists: Operations & Functions
Python-Sequences
Common Common
Operations Functions
Default value is 0 for low, 1 for step_value and (total length -1) for
high.
List1[low: high] – The sublist starts from the low index and ends at the
index one position before the high.
Concatenation (+)
The concatenation operator concatenates two lists.
Repetition (*)
The * operator concatenates a list zero or more times.
len() – len(a)
Example: len(a) returns 3
Example:
List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
List1.append (20) # Insert 20 at the end of the list
List1.insert(1, 30) #Insert 30 at the index 1
List1.extend([10, 11, 12])
List is mutable
Elements Removal -To remove an element from the list
Remove() method – If you do not know the index of the element to
be deleted.
Pop() method-Remove last element by default or the element for
which the index is mentioned.
Clear() method –Remove all elements of the list. It returns the
empty list.
Example:
List1 = ['a', 'b', 'c', 'd', 'e', 'f']
del (List1[2]) # Remove third element
del(List1) #Remove complete list
List1.remove(„d‟) #Remove element with value d
List1.pop() #Remove Last element
List1.pop(3) # Remove element at index 3
List1.clear() # remove all elements of list
Accessing Elements of List Through Loop
for i in range(len(friends)) :
friend = friends[i]
print('Happy New Year:', friend)
List Cloning/Copy
Cloning: The process to make a copy of the list to modify it in
future while keeping the original one.
Old_List = [[11, 23, 34], [40, 15, 70], 7, 8, 'a‟ ] #Creates a nested list
New_List = Old_List[:] # Creates new LIST
new_list[1][2] = 9
print('Old List:‟, Old_List)
print('ID of Old List:', id(Old_List))
print('New List:‟, New_List)
print('ID of New List:', id(New_List))
import copy
copy.copy(x) Returns a shallow copy of x (It is almost similar to the
assignment or slice operator for nested objects)
Old_List[1][1] = 'AA'
print("Old list with ID :", Old_List, id(Old_List))
print("New list with ID:", New_List, id(New_List))
Deep Copy
A deep copy creates a new object and recursively adds the copies of
nested objects present in the original elements.
The deep copy creates independent copy of original object and all
its nested objects.
Example
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list) #Creates an independent copy
print("Old list:", old_list)
print("New list:", new_list)
old_list[1][0] = 'BB‟ # This change will reflect only in Old
List
print("Old list:", old_list)
print("New list:", new_list)
Practice Questions
• Accessing List
• List Slicing
• List Cloning/Copy