In Python, Multiple values (example, Number, Character, Date etc.) can be stored in a single variable by using lists., a list is an ordered sequence of elements that can be changed or modified. A list’s items are any elements or values that are contained within it. Lists are defined by having values inside square brackets [] just as strings are defined by characters inside quotations.
Example 1 –
>>> list1 = [2,4,6,8,10,12]
>>> print(list1)
[2, 4, 6, 8, 10, 12]
Example 2 –
>>> list2 = [‘a’,’e’,’i’,’o’,’u’]
>>> print(list2)
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
Example 3 –
>>> list3 = [100,23.5,’Hello’]
>>> print(list3)
[100, 23.5, ‘Hello’]
The elements of a list are accessed in the same way as characters are accessed in a string.
Example –
>>> list1 = [2,4,6,8,10,12]
>>> list1[0]
2
>>> list1[3]
8
>>> list1[15]
IndexError: list index out of range
Lists are Mutable
In Python, lists are mutable. It means that the contents of the list can be changed after it has been created.
Example –
>>> list1 = [‘Red’,’Green’,’Blue’,’Orange’]
>>> list1[3] = ‘Black’
>>> list1
[‘Red’, ‘Green’, ‘Blue’, ‘Black’]
Concatenation
Example 1 –
>>> list1 = [1,3,5,7,9]
>>> list2 = [2,4,6,8,10]
>>> list1 + list2
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Example 2 –
>>> list3 = [‘Red’,’Green’,’Blue’]
>>> list4 = [‘Cyan’, ‘Magenta’, ‘Yellow’ ,’Black’]
>>> list3 + list4
[‘Red’,’Green’,’Blue’,’Cyan’,’Magenta’, ‘Yellow’,’Black’]
Repetition
Python allows us to replicate a list using repetition operator depicted by symbol *.
>>> list1 = [‘Hello’]
>>> list1 * 4
[‘Hello’, ‘Hello’, ‘Hello’, ‘Hello’]
Membership
Like strings, the membership operators in checks if the element is present in the list and returns True, else returns False.
>>> list1 = [‘Red’,’Green’,’Blue’]
>>> ‘Green’ in list1
True
>>> ‘Cyan’ in list1
False
Slicing
Like strings, the slicing operation can also be applied to lists.
Example 1 –
>>> list1 =[‘Red’,’Green’,’Blue’,’Cyan’, ‘Magenta’,’Yellow’,’Black’]
>>> list1[2:6]
[‘Blue’, ‘Cyan’, ‘Magenta’, ‘Yellow’]
Example 2 –
>>> list1[2:20] #second index is out of range
[‘Blue’, ‘Cyan’, ‘Magenta’, ‘Yellow’, ‘Black’]
We can access each element of the list or traverse a list using a for loop or a while loop.
List Traversal Using for Loop –
Example –
>>> list1 = [‘Red’,’Green’,’Blue’,’Yellow’, ‘Black’]
>>> for item in list1:
print(item)
Output:
List Traversal Using while Loop –
Example –
>>> list1 = [‘Red’,’Green’,’Blue’,’Yellow’, ‘Black’]
>>> i = 0
>>> while i < len(list1):
print(list1[i])
i += 1
Output:
len()
list()
Example –
>>> list1 = list()
>>> list1
[ ]
>>> str1 = ‘aeiou’
>>> list1 = list(str1)
>>> list1
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
append()
Example –
>>> list1 = [10,20,30,40]
>>> list1.append(50)
>>> list1
[10, 20, 30, 40, 50]
>>> list1 = [10,20,30,40]
>>> list1.append([50,60])
>>> list1
[10, 20, 30, 40, [50, 60]]
extend()
Example –
>>> list1 = [10,20,30]
>>> list2 = [40,50]
>>> list1.extend(list2)
>>> list1
[10, 20, 30, 40, 50]
insert()
Example –
>>> list1 = [10,20,30,40,50]
>>> list1.insert(2,25)
>>> list1
[10, 20, 25, 30, 40, 50]
>>> list1.insert(0,5)
>>> list1
[5, 10, 20, 25, 30, 40, 50]
count()
Example –
>>> list1 = [10,20,30,10,40,10]
>>> list1.count(10)
3
>>> list1.count(90)
0
index()
Example –
>>> list1 = [10,20,30,20,40,10]
>>> list1.index(20)
1
>>> list1.index(90)
ValueError: 90 is not in list
remove()
Example –
>>> list1 = [10,20,30,40,50,30]
>>> list1.remove(30)
>>> list1
[10, 20, 40, 50, 30]
>>> list1.remove(90)
ValueError:list.remove(x):x not in list
pop()
Example –
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop(3)
40
>>> list1
[10, 20, 30, 50, 60]
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop()
60
>>> list1
[10, 20, 30, 40, 50]
reverse()
Example –
>>> list1 = [34,66,12,89,28,99]
>>> list1.reverse()
>>> list1
[ 99, 28, 89, 12, 66, 34]
>>> list1 = [ ‘Tiger’ ,’Zebra’ , ‘Lion’ , ‘Cat’ ,’Elephant’ ,’Dog’]
>>> list1.reverse()
>>> list1
[‘Dog’, ‘Elephant’, ‘Cat’, ‘Lion’, ‘Zebra’, ‘Tiger’]
sort()
Example –
>>>list1 = [‘Tiger’,’Zebra’,’Lion’, ‘Cat’, ‘Elephant’ ,’Dog’]
>>> list1.sort()
>>> list1
[‘Cat’, ‘Dog’, ‘Elephant’, ‘Lion’, ‘Tiger’, ‘Zebra’]
>>> list1 = [34,66,12,89,28,99]
>>> list1.sort(reverse = True)
>>> list1
[99,89,66,34,28,12]
sorted()
Example –
>>> list1 = [23,45,11,67,85,56]
>>> list2 = sorted(list1)
>>> list1
[23, 45, 11, 67, 85, 56]
>>> list2
[11, 23, 45, 56, 67, 85]
min()
Example –
>>> list1 = [34,12,63,39,92,44]
>>> min(list1)
12
max()
Example –
>>> list1 = [34,12,63,39,92,44]
>>> max(list1)
92
sum()
Example –
>>> list1 = [34,12,63,39,92,44]
>>> sum(list1)
284
Example –
>>> list1 = [1,2,’a’,’c’,[6,7,8],4,9]
>>> list1[4]
[6, 7, 8]
Example –
>>> list1 = [1,2,3]
>>> list2 = list1
>>> list1
[1, 2, 3]
>>> list2
[1, 2, 3]
List as Argument to a Function
Whenever a list is passed as an argument to a function, we have to consider two scenarios:
(A) A modification to the list in the function will be mirrored back in the calling function, which allows for changes to the original list’s elements.
Q. Program to increment the elements of a list. The list is passed as an argument to a function.
#Program
#Function to increment the elements of the list passed as argument
def increment(list2):
for i in range(0,len(list2)):
list2[i] += 5
print(‘Reference of list Inside Function’,id(list2))
list1 = [10,20,30,40,50]
print(“Reference of list in Main”,id(list1))
print(“The list before the function call”)
print(list1)
increment(list1)
print(“The list after the function call”)
print(list1)
Output:
Reference of list in Main 70615968
The list before the function call
[10, 20, 30, 40, 50]
Reference of list Inside Function 70615968
The list after the function call
[15, 25, 35, 45, 55]
(B) If the list is given a new value inside the function, a new list object is generated and it becomes the local copy of the function. Any updates made inside the local copy of the function are not updated in the calling function.
Q. Program to increment the elements of the list passed as parameter.
#Program
#Function to increment the elements of the list passed as argument
def increment(list2):
print(“\nID of list inside function before assignment:”, id(list2))
list2 = [15,25,35,45,55]
print(“ID of list changes inside function after assignment:”, id(list2))
print(“The list inside the function after assignment is:”)
print(list2)
list1 = [10,20,30,40,50]
print(“ID of list before function call:”,id(list1))
print(“The list before function call:”)
print(list1)
increment(list1) #list1 passed as parameter to function
print(‘\nID of list after function call:’,id(list1))
print(“The list after the function call:”)
print(list1)
Output:
ID of list before function call: 65565640
The list before function call:
[10, 20, 30, 40, 50]
ID of list inside function before assignment:65565640
ID of list changes inside function after assignment:65565600
The list inside the function after assignment is:
[15, 25, 35, 45, 55]
ID of list after function call: 65565640
The list after the function call:
[10, 20, 30, 40, 50]
84 videos|19 docs|5 tests
|
|
Explore Courses for Grade 11 exam
|