List and Tuple Worksheet Solution
List and Tuple Worksheet Solution
Worksheet-List Manipulation
Solution
Q1. Start with the list[8,9,10]. Do the following using list functions
(a) Set the second entry (index 1) to 17
(b) Add 4, 5 and 6 to the end of the list.
(c) Remove the first entry from the list.
(d) Sort the list. e) Double the list. f)Insert 25 at index 3
Ans:
a) list[1]=17
b) list.append(4) list.append(5) list.append(6)
c) list.pop(0)
d) list. sort()
e) list=list*2
f) list. insert(3,25)
Q2. If a is [1, 2, 3], what is the difference (if any) between a*3 and [a, a, a]?
Ans: a*3 will produce [1,2,3,1,2,3,1,2,3], means a list of integers and [a, a, a] will produce [[1,2,3],[1,2,3],[1,2,3]], means list of
lists
Q3.If a is [1, 2, 3], is a *3 equivalent to a + a + a?
Ans: Yes, Both a*3 and a+a+a will produce same result.
Q4.How are lists different from strings when both are sequences?
Ans: Lists are similar to strings in many ways like indexing, slicing, and accessing individual elements but they are different in the
sense that Lists are mutable while strings are immutable.
i. In consecutive locations, strings store the individual characters while list stores the references of its elements.
ii.Strings store single type of elements-all characters while lists can store elements belonging to different types.
Q5.Differentiate between
1. Pop() and remove()
pop() and del remove()
del is a keyword and pop () is a built in method remove () method delete values or object from the list
both deletes values or object from the list using an using value. The remove() method removes the first
index. pop() returns deleted value. matching value from the list.
numbers = [1, 2, 3, 2, 3, 4, 5] numbers = [1, 2, 3, 2, 3, 4, 5]
del numbers[2] numbers. remove(2)
print(numbers) print(numbers)
numbers.pop() print(numbers. remove(1))
print(numbers) print(numbers)
print(numbers.pop(-1))
print(numbers)
OUTPUT OUTPUT
[1, 2, 2, 3, 4, 5] [1, 3, 2, 3, 4, 5]
[1, 2, 2, 3, 4] None # not return any value
4 [3, 2, 3, 4, 5]
[1, 2, 2, 3]
2. Append() and extend()
Append() Extend()
It adds a single item to the end of the list It adds one list at the ends of another list.
my_list = ['python', 'for'] my_list = ['python', 'is']
my_list.append('learning') my_list.extend('easy')
print (my_list) print (my_list)
my_list.append([1,2]) my_list.extend([1,2])
print(my_list) print(my_list)
OUTPUT OUTPUT
['python', 'for', 'learning'] ['python', 'is', 'e', 'a', 's', 'y']
['python', 'for', 'learning', [1, 2]] ['python', 'is', 'e', 'a', 's', 'y', 1, 2]
3. Clear() and del
clear(): clear() method in python is used to empty the entire list. this can be used if someone wants an empty list for any other
purpose.
Eg:
numbers = [1, 2, 3, 2, 3, 4, 5]
print(numbers)
numbers.clear()
print(numbers)
o/p
[1, 2, 3, 2, 3, 4, 5]
[]
3. Sort() and sorted()
Sort() Sorted()
sort() function is very similar to sorted() but unlike sorted() method sorts the given sequence either in
sorted it returns nothing and makes changes to the ascending order or in descending order and always
original sequence. Moreover, sort() is a method of return the a sorted list. This method does not effect the
list class and can only be used with lists. original sequence. sequence list, tuple, string or
dictionary.
numbers = [1, 3, 4, 2] numbers =[1, 3, 4, 2]
numbers. sort() numbers1=sorted(numbers)
print(numbers) print(numbers1)
numbers. sort(reverse=True) numbers={4:'a',1:'b',3:'c'}
print(numbers) numbers1=sorted(numbers, reverse=True)
print(numbers1)
numbers="1265"
numbers1=sorted(numbers, reverse=True)
print(numbers1)
OUTPUT OUTPUT
[1, 2, 3, 4]
[1, 2, 3, 4] [4, 3, 1]
[4, 3, 2, 1] ['6', '5', '2', '1']