Posts

Showing posts from November, 2018

Map function in Python

Image
  The  map ()  function  executes a specified  function  for each item in a iterable. The item is sent to the  function  as a parameter. Code Example >>> l1 = [1, 2, 3, 4, 5] >>> l2 = [5, 4, 3, 2, 1] >>> l3 = [5, 4, 3, 2, 1] >>> def cal(a=0, b=0, c=0):                  print(a+b+c) >>> x = map(cal, l1, l2, l3) >>> x <map object at 0x03FB6100> >>> x = list(x) Output: 11 10 9 8 7 Quiz Level 1 Quiz Level 2 Request Me

Deque in Python

Image
  Concept Deque (Doubly Ended Queue) in python is implemented using the module “ collections “. Deque is preferred over the list  in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an  O(1)  time complexity for append and pop operations as compared to list which provides O(n) time complexity. Code import collections dq = collections.deque([20, 30, 40]) print(dq) # added in right end dq.append(50) print(dq) # added in left end dq.appendleft(6) print(dq) # deleteing from right end dq.pop() # deleting from left end dq.popleft() Screenshot : Quiz Level 1 Quiz Level 2 Request Me  

Enumerate in Python

Image
  Code A lot of times when dealing with iterators, we also get a need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task. The enumerate () method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using the list() method. >>> l = [i for i in range(1, 100, 10)] >>> l [1, 11, 21, 31, 41, 51, 61, 71, 81, 91] >>>  >>> for i, j in enumerate(l): print("i is :{}, j is :{}".format(i, j)) i is :0, j is :1 i is :1, j is :11 i is :2, j is :21 i is :3, j is :31 i is :4, j is :41 i is :5, j is :51 i is :6, j is :61 i is :7, j is :71 i is :8, j is :81 i is :9, j is :91 Quiz Level 1 Quiz Level 2 Request Me