Write Python program to perform following operations on Lists: Create list, Access list, Update list (Add item, Remove item), Delete list
A list is a collection which is ordered and changeable.
A list is a collection of items or elements; the sequence of data in a list is ordered.
#totalprogrammingcode
The elements or items in a list can be accessed by their positions i.e. indices
In Python lists are written with square brackets. A list is created by placing all the
items (elements) inside a square brackets [ ], separated by commas.
Lists are mutable. The value of any element inside the list can be changed at any
point of time.
Write Python program to perform following operations on Lists: Create list, Access list, Update list (Add item, Remove item), Delete list |
The index always starts with 0 and ends with n-1, if the list contains n elements.
a) Creating List: Creating a list is as simple as putting different comma-separated
values between square brackets.
Example:
>>>List1=[‘Java’,’Python’,’Perl’]
>>>List2=[10,20,30,40,50]
b) Accessing List: To access values in lists, use the square brackets for slicing along
with the index or indices to obtain value available at that index.
Example:
>>>List2
[10,20,30,40,50]
>>>List2[1]
20
>>>List2[1:3]
[20,30]
>>>List2[5]
Traceback (most recent call last):
File "<pyshell#71>", line 1, in <module>
List2[5]
IndexError: list index out of range
c) Updating List: You can update single or multiple elements of lists by giving the
slice on the left-hand side of the assignment operator, and you can add to elements in a
list with the append() method.
>>>List2
[10,20,30,40,50]
>>>List2[0]=60 #Updating first item
[60,20,30,40,50]
>>>List2[3:4]=70,80
>>>[60,20,30,70,80,50]
i) We can add one item to a list using append() method or add several items using
extend() method.
>>> list1=[10,20,30]
>>> list1
[10, 20, 30]
>>> list1.append(40)
>>> list1
[10, 20, 30, 40]
>>> list1.extend([60,70])
>>> list1
[10, 20, 30, 40, 60, 70]
ii) We can also use + operator to combine two lists. This is also called
concatenation
>>> list1=[10,20,30]
>>> list1
[10, 20, 30]
#totalprogrammingcode
>>> list1+[40,50,60]
[10, 20, 30, 40, 50, 60]
iii) The * operator repeats a list for the given number of times.
>>> list2 ['A', 'B']
>>> list2 *2
['A', 'B', 'A', 'B']
iv) We can insert one item at a desired location by using the method insert()
>>> list1
[10, 20]
>>> list1.insert(1,30)
>>> list1
[10, 30, 20]
d) Deleting List: To remove a list element, you can use either the del statement if you
know exactly which element(s) you are deleting or the remove() method if you do not
know.
i) Del Operator: We can delete one or more items from a list using the keyword del. It
can even delete the list entirely. But it does not store the value for further use.
Example:
>>> list=[10,20,30,40,50]
>>> del list[2]
>>> list
[10, 20, 40, 50]
ii) Remove Operator: We use the remove operator if we know the item that we want
to remove or delete from the list (but not the index)
Example:
>>> list=[10,20,30,40,50]
>>> list.remove(30)
>>> list
[10, 20, 40, 50]
Examples
1. Write a Python program to sum all the items in a list
no = [10,12,13,14,15]
ans = 0
for i in range(0,len(no)):
ans = ans+ no[i]
print("The sum of digit in list :",ans)
2. Write a Python program to find common items from two lists
list1 = [10,20,30,40,50]
list2 = [10,30,70,30,90]
for i in range(len(list1)):
num = list1[i]
for z in range(len(list2)):
num2 = list2[z]
if(num == num2):
print("The common item in list :",num)
3. Write a Python program to reverse a list.
list = [10,20,30,40,50]
list.reverse()
print("Reverse list :",list)
4. Write a Python program to get the largest number from a list.
list = [10,20,30,40,50]
list1 = max(list)
print("Largest item in list :",list1)