Python Programming
About Lesson

Length of List
The in-built len() function can be used to get the number of items in the list.

#Example
fruits = [“apple”, “banana”, “cherry”]
print(len(fruits))

Change the value of an Item Value
Using positive or negative indices, we can modify the list data items.

#Example
fruits = [“apple”, “banana”, “cherry”]
fruits[1] = “watermelon”
print(fruits)

#Example
sub = [‘Maths’,’physics’, ‘chemistry’];
print (“Initial value at index 2 is: “)
print (sub[2])
sub[2] = ‘Oganic Chemistry’;
print (“Updated value at index 2 is: “)
print (sub[2])

Add new Items
New data items can be added to the existing list with the following methods.
1. At the end of the list using append() function
2. At the specified index using insert() function

#Example
fruits = [“apple”, “banana”, “cherry”]
fruits.append(“orange”)
print(fruits)

#Example
fruits = [“apple”, “banana”, “cherry”]
fruits.insert(1, “orange”)
print(fruits)

Remove Item List Elements
The existing data elements of list can be removed by the following ways:
1. Using del command: remove value at a specified index or entire list
2. Using remove() function: remove specified value
3. Using pop() function: remove last element or value at specified index
4. Using clear() function: empty list

#Example to remove the value at specified index
sub = [‘maths’, ‘physics’, ‘chemistry’]
print (sub)
del sub[2];
print (“After deletion value at index 2 : “)
print (list1)

#Example to remove a specified value
fruits = [“apple”, “banana”, “cherry”]
fruits.remove(“banana”)
print(fruits)

#Example to remove the list completely
fruits = [“apple”, “banana”, “cherry”]
del fruits

#example code to remove last element
fruits = [“apple”, “banana”, “cherry”]
fruits.pop()
print(fruits)

#example code to remove last element
fruits = [“apple”, “banana”, “cherry”]
fruits.pop(2)
print(fruits)

#example code to empty the list
fruits = [“apple”, “banana”, “cherry”]
fruits.clear()
print(fruits)

Copy a List
A list cannot be copied directly into another list. If you do so, the new name will act as a reference to the existing list.

fruits1 = [“apple”, “banana”, “cherry”]
fruits2=fruits1
fruits2[2]= “apple”
print(fruits1)

Following are the in-built methods to copy a list:
1. Using copy() method
2. Using list() method

# Example code create a copy of the existing list using copy() method
fruits1 = [“apple”, “banana”, “cherry”]
fruits2 = fruits1.copy()
print(fruits1)
print(fruits2)
fruits2[2]= “apple”
print(fruits1)
print(fruits2)

 

#Example code make copy of the list using list() method
fruits1 = [“apple”, “banana”, “cherry”]
fruits2 = list(fruits1)
print(fruits1)
print(fruits2)
fruits2[2]= “apple”
print(fruits1)
print(fruits2)

Joining Two Lists
Two or more lists can be concatenated or joined into a single list. The following ways can be used to join lists:
1. Using + operator
2. Using extend() method

#Example code to join two lists using + operator by creating a new list
AlphList = [“a”, “b” , “c”]
NumList = [1, 2, 3]
NewList = AlphList + NumList
print(NewList)

#Example code to add one list elements into another existing elements
AlphList = [“a”, “b” , “c”]
NumList = [1, 2, 3]
AlphList.extend(NumList)
print(AlphList)
print(NumList)

#Example code to create new list by repetitively adding the elements of existing list
AlphList = [“a”, “b” , “c”]
NewList = AlphList * 2
NumList=[1,2,3] * 3
print(NewList)
print(NumList)

You cannot copy content of this page