Chapter 13 Lists, Dictionaries and Tuples Solutions
Question - 11 : - Give an example to update single or multiple elements of lists
Answer - 11 : -
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 elements in a list with the append( ) method. Following is a simple example:
# !/user/bin/python
listl = [‘physics’, ‘chemistry’, 1997, 2000];
print “Value available at index 2 :”
print list[2];
list[2] = 2001;
print “New value available at index 2 :”
print list [2];
Note: append)) method is discussed in subsequent section.
When the above code is executed, it produces the following result:
Value available at index 2 :
1997
New value available at index 2 :
2001
Question - 12 : - Give an example to remove list element.
Answer - 12 : -
To remove a list element, you can use either I the del statement if you know exactly which ‘ element(s) you are deleting or the remove I ( ) method if you do not know. Following is a simple example:
# !/user/bin/python
list1 = [‘physics’, ‘chemistry’, 1997, 2000];
print list1;
del list1 [2];
print “After deleting value at index 2 :”
print list1;
When the above code is executed, it produces the following result:
[‘physics’, ‘chemistry’, 1997, 2000];
After deleting value at index 2;
[‘physics’, ‘chemistry’, 2000]
Question - 13 : - Write the output of the given python code :
# ! ‘user/bin’pvthon
aList1 = [123, ‘xvz’, zara’, abc’];
print “Index for xyz : ” aList. index) ‘xyz’);
print “Index for zara :”, aList. index(‘zara’);
Answer - 13 : -
This will produce the following result:
Index for xyz : 1 Index for xxx : 2
Question - 14 : - Write the output of the given python code :
#!/user/bin/python
aList1 = [123, ‘xyz’, ‘zara’, ‘abc’];
aList.insert (3,2009) print “Final Lista List
Answer - 14 : -
Output:
Final List: [123, ‘xyz’, ’zara1, 2009,’abc1’]
Question - 15 : - Write the output of the following code
A = [2, 4, 6, 8,10]
L = len (A)
S = o
for I in range (1, L, 2):
S + = A[I]
print “Sum=”, S
Answer - 15 : -
Sum = 12
Justification:
A[1] = 4 step size = 2
A[3] = 8
S = 4 + 8 = 12
Question - 16 : - How are lists different from strings when both are sequences ?
Answer - 16 : -
The lists and strings are different in following ways :
(a) The lists are mutable sequences while strings are immutable.
(b) Strings store single type of elements, all characters while lists can store elements belonging to different types.
(c) In consecutive locations, strings store the individual characters while list stores the references of its elements.
Question - 17 : - Write a program to calculate and display the sum of all the odd numbers in the list.
Answer - 17 : -
pos = 0
sum = 0
while pos < len (L):
if L[pos] %2 = = 1 :
sum = sum + L [pos]
pos = pos + 1
print sum
Question - 18 : - Define a function overlapping ( ) that takes two lists and returns True if they have at least one member in common, False otherwise.
Answer - 18 : -
def overlapping (a, b):
11 = len (a)
12 = len (b)
for i in range (11):
for j in range (12):
if a[i] == b[j] :
return True
else
return False
Question - 19 : - What is string in Python and how can we access values in list.
Answer - 19 : -
The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number – its position or index. The first index is zero, the second index is one, and so forth. Python has six built -in types of sequences, but the most common ones are lists and tuples.
There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.
Python Lists:
The list is a most versatile datatype available in Python which can be written as a list of comma- seprated values (items) between brackets. Good thing about a list is that items in a list need not all have the same type.
Creating a list is as simple as putting different comma-separated values between square brackets. For example:
list1 = [‘physics’, ‘chemistry’, 1997,2000];
list2 = [1, 2, 3,4,5];
list3 = [“a”, “b”, ”c”, “d”];
Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
Accessing Values in Lists :
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. Following is a simple example :
# !/user/bin/python
list = [‘physics’, ‘chemistry’, 1997,2000];
list2 = [1,2, 3,4,5,6, 7];
print “list1 [0]:”, list1 [0]
print “list2[1:5]:”, list2[1:5]
list 2[1:5]: [2, 3, 4, 5]
Question - 20 : - How can we update and delete list in python.
Answer - 20 : -
Updating Lists:
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. Following is a simple example:
# !/user/bin/python
list = [‘physics’, ‘chemistry’, 1997, 2000];
print “Value available at index 2 :”
print list[2];
list [2] = 2001;
print “New value available at index 2 :”
print list [2];
When the above code is executed it produces the following result:
Value available at index 2 :
1997
New value available at index 2 :
2001
Delete List Elements:
To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting of the remove() method if you do not know.
Following is a simple example :
# !/user/bin/python
list1 = [‘physics’, ‘chemistry’, 1997, 2000];
print list1; del list1 [2];
print “After deleting value at index 2:”
print list1;
When the above code is executed, it produces the following result:
[‘physics’, ‘chemistry’, 1997, 2001]
After deleting value at index 2 :
[‘physics’, ‘chemistry’, 2000]