Class 11 informatics practices with python list notes
In Python, a list is a kind of container that contains a collection of any kind of values.
A List is a mutable data type which means any value from the list can be changed. For changed values , Python does not create a new list.
List is a sequence like a string and a tuple except that list is mutable whereas string and tuple are immutable.
In this chapter we will see the manipulation on lists. We will see creation of list and various operation on lists via built in functions.
List Creation
List is a standard data type of Python. It is a sequence which can store values of any kind.
List is represented by square brackets “ [ ] “
For ex -
[ ] Empty list
[1, 2, 3] integers list
[1, 2.5, 5.6, 9] numbers list (integer and float)
[ ‘a’, ‘b’, ‘c’] characters list
[‘a’, 1, ‘b’, 3.5, ‘zero’] mixed values list
[‘one’, ’two’, ’three’] string list
In Python, only lists and dictionaries are mutable data types, rest of all the data types are immutable data types.
List can be created in following ways-
Empty list - L = [ ]
list can also be created with the following statement
L = list( )
Another method to create list
Long list11 = [0, 2, 4, 6, 8, 10 ,12 ,14 ,16 ,18 ,20 ]
Nested list - L = [ 3, 4, [ 5, 6 ], 7]
As we know that when we try to take input from the user it is treated as a string even numbers also treated as string to convert the this string to number we use As we have seen in the example That when we have supplied values as numbers to a list even then They have automatically converted to string – If we want to pass values to a list in numeric form then we have to write following function -
eval(input())
L=eval(input(“Enter list to be added “))
Also we can use type casting to do mathematical operations. For example
Accessing a List
• First we will see the similarities between a List and a String.
• List is a sequence like a string.
• List also has index of each of its element.
• Like string, list also has 2 index, one for forward indexing (from 0, 1, 2, 3, ….to n-1) and one
for backward indexing(from -n to - 1).
• In a list, values can be accessed like string.
To access an element, use square brackets with the index [] value of that element. We may also use negative index value to access elements starting from the last element in the list, having index value -0.
Lists are Mutable In Python, lists are mutable. It means that the contents of the list can be changed after it has been created.
#List list1 of colors
>>> list1 = ['Red','Green','Blue','Orange']
#change/override the fourth element of list1
>>> list1[3] = 'Black'
>>> list1
#print the modified list list1
['Red', 'Green', 'Blue', 'Black']
Difference between a List and a String
Main difference between a List and a string is that string is immutable whereas list is mutable.
Individual values in string can’t be change whereas it is possible with list
Let us see the example:
Traversal of a list
Traversal of a list means to access and process each and every element of that list.
Traversal of a list is very simple with for loop –
List Methods and Built-in Functions The data type list has several built-in methods that are useful in programming.
List Manipulation In this chapter, we have learnt to create a list and the different ways to manipulate lists. In the following programs, we will apply the various list manipulation methods.
1. Append an element
2. Insert an element
3. Append a list to the given list
4. Modify an existing element
5. Delete an existing element from its position
6. Delete an existing element with a given value
7. Sort the list in the ascending order
8. Sort the list in descending order
9. Display the list.
1. Append an element
myList = [22,4,16,38,13] #myList having 5 elements
element = eval(input("Enter the element to be appended: ")) myList.append(element) print("The element has been appended\n")
#insert an element at desired position
myList = [22,4,16,38,13] #myList having 5 elements
element = eval(input("Enter the element to be inserted: "))
pos = int(input("Enter the position:")) myList.insert(pos,element)
print("The element has been inserted\n")
#append a list to the given list
myList = [22,4,16,38,13] #myList having 5 elements
newList = [12,34,56,78]
myList.extend(newList)
print("The list has been appended\n",myList)
# Modify an existing element
myList = [22,4,16,38,13]
i = int(input("Enter the position of the element to be modified: "))
if i < len(myList):
newElement = eval(input("Enter the new element: "))
oldElement = myList[i]
myList[i] = newElement
print("The element",oldElement,"has been modified\n new list is ",myList)
else:
print("Position of the element is more then the length of list")
#Delete an existing element from its position
myList = [22,4,16,38,13]
i = int(input("Enter the position of the element to be deleted: "))
if i < len(myList):
element = myList.pop(i)
print("The element",element,"has been deleted\n",myList)
else:
print("\nPosition of the element is more then the length of list")
# Delete an existing element with a given value
myList = [22,4,16,38,13]
element = int(input("\nEnter the element to be deleted: "))
if element in myList:
myList.remove(element)
print("\nThe element",element,"has been deleted\n",myList)
else:
print("\nElement",element,"is not present in the list")
#Sort the list in the ascending order
print("\nThe list before sort",myList)
myList.sort()
print("\nThe list after sort",myList)
#Sort the list in th decending order
myList = [22,4,16,38,13]
print("\nThe list before sort",myList)
myList.sort(reverse = True)
print("\nThe list after sort in decending order",myList)
0 Comments