Saturday, February 1, 2020

List Manipulation practice

List Manipulation

Question: Write the output of the given Python code.
(a)  list1 = [‘physics’, ‘chemistry’, 1997, 2000];
list2 = [1,2,3,4,5,6, 7];
print “list1[0]”, list1[0]
Answer:
list1[0]: physics

(b)  listl = [‘physics’, ‘chemistry’, 1997,2000];
list2 = [1,2, 3,4,5, 6, 7];
print “list2[l:5[ :”, list2[l:5]
Answer:
list2[1:5[: [2,3,4,5]

(c)  list1 = [‘physics’, ‘chemistry’, 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7];
print “list1 [0]:”,
list1 [0] print “list2[1 :5]:”, list2[1:5]
Answer
list1 [0]: physics
list2[1:5] : [2, 3, 4, 5]

(d)  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];
Answer
Value available at index 2 : 1997
New value available at index 2 : 2001

(e)  list1 = [‘physics’, ‘chemistry’, 1997, 2000];
print list1;
del list1 [2];
print “After deleting value at index 2 :”
print list1;
Answer
[‘physics’, ‘chemistry’, 1997, 2000];
After deleting value at index 2;
[‘physics’, ‘chemistry’, 2000]

(f)   aList = [123, ‘xyz’, ‘zara’, ‘abc’, 123];
bList = [2009, ‘manni’];
aList.extend (bList)
print “Extended List :”, aList;
Answer
Extended List : [123, ‘xyz’, ‘zara’, ‘abc’, 123, 2009, ‘manni’]


(g)  aList1 = [123, ‘xvz’, zara’, abc’];
print “Index for xyz : ” aList. index( ‘xyz’);
print “Index for zara :”, aList. index(‘zara’);
Answer
Index for xyz : 1 Index for xxx : 2

(h)  aList = [123, ‘xyz’, ‘zara’, ‘abc’];
aList.insert (3,2009)
print “Final List:”, aList
Answer:
Final List: [123, ‘xyz’, ‘zara’, 2009, ‘abc’]

            (i)    aList1 = [123, ‘xyz’, ‘zara’, ‘abc’];
            print “A List:”, aList.pop()
           print “B List:”, aList.pop(2)
Answer:
A List: abc B List: zara

           (j)    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:
Sum = 12

           (k)  A= (10:1000,20:2000,30:3000,40:4000, 50:5000}
           print A.items()
           print A.keys()
           print A.values()
Answer:
[(40, 4000), (10, 1000), (20, 2000), (50, 5000), (30, 3000)]
[40,10, 20, 50, 30]
[4000,1000, 2000,5000,3000]




Question:
How can we remove list element ?
Answer:
To remove a list element, you can use either the del statement or the remove method

Question:
What is the use of len(list)) in Python?
Answer:
It gives the total length of the list.

Question:
Describe max(list) method.
Answer:
It returns item from the list with max value.

Question:
Describe min(list) method.
Answer:
It returns item from the list with min value.

Question:
What is the use of list(seq) in Python ?
Answer:
It converts a tuple into list.

Question:
What do you mean by list.append(obj)
Answer:
Appends object obj to list

Question:
What do you mean by list.count(obj)
Answer:
Returns count how many times obj occurs in list

Question:
Explain list.extend(seq)
Answer:
Appends the contents of seq to list.

Question:
Is list.reverse() method return a value?
Answer:
This method does not return any value but reverse the given object from the list

Question:
Which method is used to sort objects of list.
Answer:
sort ()

Question:
Which function is used to reverse objects of list in place.
Answer:
list.reverse ()

Question:
Which function is use for removing object obj from list.
Answer:
list.remove(obj)

Question:
Which function is use for returning the lowest index in list that obj appears.
Answer:
List index (obj).

Question:
How are lists different from strings when both are sequences ?
Answer:
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:
Write a program to calculate and display the sum of all the odd numbers in the list.
Answer:
pos = 0
sum = 0
while pos < len (L):
   if L[pos] %2 = = 1 :
   sum = sum + L [pos]
   pos = pos + 1
print sum

Question:
Write a program to perform various list operations after displaying menu.
Answer:
ch = 0
list = [ ]
while true :
print “List Menu”
print “1. Adding”
print “2.Modify”
print “3.Delete”
print “4.Sort list”
print “5.Display list”
print “6.Exit”
ch = int (input (“Enter your choice :”))
if ch = = 1 :
  print “1.Add element”
  print “2.Add a List”
  ch1 = int (input(“Enter choice 1 or 2:”))
  if chi = = 1:
    item = int(input (“Enter element:”))
    pos = int (input(“Enter position to add : “))
    list.insert (pos, item)
  elif chi = = 2 :
    list2 = eval (input (“Enter list:”))
    list.extend (lst2)
  else:
    print “Valid choices are 1 or 2”
    print “Successfully added”
elif ch = = 2 :
   pos = int (input(“Enter position :”))
   intem = int (input(“Enter new value :”))
   old = list[pos]
   list[.pos] = item
   print old, ‘modified with vlaue”, item
elif ch = = 3:
  print “1.Delete element by position”
  print “2.Delete element by value”
  ch1 = int (input (“Enter you choice 1 or 2’))
  if chi = = 1:
    pos = int (input (“Enter position :”))
    item = list.pop (pos)
    print item, “deleted”
  elif ch1 = = 2:
    item = int (input (“Enter element:”))
    pos = list.remove (item)
    print “Succcessfully delted”
  else :
    print “Valid choices are 1 or 2”
elif ch = = 4 :
  print “l.Ascending”
  print “2.Descending”
  chi = int (input (“Enter choice 1 or 2”))
  if chi = = 1:
    list. sot ()
  elif ch1 = = 2:
    list.sort (reverse = True)
  else :
    print “Valid choices are 1 or 2”
elif ch = = 5:
  print list
elif ch = = 6:
  break
else :
  print “valid choice 1 to 6”

No comments:

Post a Comment