Difference between del, remove and pop methods in python list

del to remove an element by index

pop() to remove it by index if you need the returned value

remove() to delete an element by value.

The latter requires searching the list, and raises ValueError if no such value occurs in the list.

When deleting index i from a list of n elements, the computational complexities of these methods are

del     O(n - i)
pop     O(n - i)
remove  O(n)

Read the examples to understand quickly:

remove removes the first matching value, not a specific index:

>>> a = [0, 2, 2, 3]
>>> a.remove(2)
>>> a
[0, 2, 3]

del removes a specific index:

>>> a = [3, 2, 2, 1]
>>> del a[1]
[3, 2, 1]

and pop returns the removed element:

>>> a = [4, 3, 5]
>>> a.pop(1)
>>> a
[4, 5]

Their error modes are different too:

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range

C operators precedence and its associativity

Precedence of operators

If more than one operators are involved in an expression, C language has a predefined rule of priority for the operators. This rule of priority of operators is called operator precedence.

In C, precedence of arithmetic operators( *, %, /, +, -) is higher than relational operators(==, !=, >, <, >=, <=) and precedence of relational operator is higher than logical operators(&&, || and !).

Example of precedence

(1 > 2 + 3 && 4)
This expression is equivalent to:
((1 > (2 + 3)) && 4)
i.e, (2 + 3) executes first resulting into 5
then, first part of the expression (1 > 5) executes resulting into 0 (false)
then, (0 && 4) executes resulting into 0 (false)



Associativity of operators

If two operators of same precedence (priority) is present in an expression, Associativity of operators indicate the order in which they execute.

Example of associativity

1 == 2 != 3

Here, operators == and != have same precedence. The associativity of both == and != is left to right, i.e, the expression on the left is executed first and moves towards the right.

Thus, the expression above is equivalent to :

((1 == 2) != 3)
i.e, (1 == 2) executes first resulting into 0 (false)
then, (0 != 3) executes resulting into 1 (true)



The table below shows all the operators in C with precedence and associativity.

Note: Precedence of operators decreases from top to bottom in the given table.

Summary of C operators with precedence and associativity
Operator Meaning of operator Associativity
Functional call
Array element reference
Indirect member selection
Direct member selection
Left to right


Logical negation
Bitwise(1 ‘s) complement
Unary plus
Unary minus
Dereference Operator(Address)
Pointer reference
Returns the size of an object
Type cast(conversion)
Right to left
Left to right
Binary plus(Addition)
Binary minus(subtraction)
Left to right
Left shift
Right shift
Left to right
Less than
Less than or equal
Greater than
Greater than or equal
Left to right
Equal to
Not equal to
Left to right
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?: Conditional Operator Right to left
Simple assignment
Assign product
Assign quotient
Assign remainder
Assign sum
Assign difference
Assign bitwise AND
Assign bitwise XOR
Assign bitwise OR
Assign left shift
Assign right shift
Right to left
, Separator of expressions Left to right


Enabling Tab to Autocomplete the Variable names and Function names in Python Interactive Shell

One of its best features is the interactive shell where we can experiment all we want. Let’s say you open up a shell, declare a bunch of variables and want to operate on them. You don’t want to type the full variables names over and over again, right? Also, it’s difficult to remember the full names of all the inbuilt methods and functions as well. Since we are playing around with the same variables and inbuilt functions, it would be nice to have an autocomplete feature that can complete the variable and function names for us. Fortunately, Python provides that nifty little feature! Let’s see how we can enable it here.  

Create file named “.pythonrc” in your home directory by running the following command on your terminal:

$ vim ~/.pythonrc

Add the following lines in that file:

import rlcompleter, readline
readline.parse_and_bind("tab: complete")

Save and close the file. Now open your “~/.profile” file if you are on OS X (“~/.bashrc” file if you are on Ubuntu), go to the last line, and add the following line after that:

export PYTHONSTARTUP="~/.pythonrc"

Save and close this file. We need to tell our “.profile” file that we have updated the environment variables. So let’s restart it by running the following command:

$ source ~/.profile

We are now ready to give it a spin. Open up the Python shell by typing “python” on your terminal. Let’s declare a string variable:

>>> my_variable = "Hello world"
>>> my

In the second line, if you type “my” and hit “tab”, it will autocomplete it to “my_variable”. You can type “my_variable.” (notice the trailing “.”) and hit tab twice, it will display all the inbuilt methods that can be applied to a string variable.

If you import a module, you can see a list of all its methods as well:

>>> import os
>>> os.

Display all 234 possibilities? (y or n)

It will display a list of all the methods you can use with the “os” module.

Python Lambda (or a.k.a Anonymous Function), Filter, Map and Reduce

Python Lambda function or Anonymous Function

The lambda function is enables us to create anonymous functions. These are functions without a name. This reminds me of the famous Clint Eastwood character in the Dollars Trilogy i.e. Man With No Name. Lambda functions are basically functions that we can throw away because their scope is very localized. Why would we use such a function? Well, lambda is very frequently used with filter, map, and reduce. So it’s important to understand how it works!

Let’s go into the Python shell to understand this. Go to your terminal and type “python”:

>>> f = lambda a, b, c: a * b + c
>>> f(3,4,5)

It’s pretty simple, right? You define a function with some input arguments and the output definition. We use the keyword “lambda” to indicate that it’s a lambda function. In this case, ‘a’, ‘b’, and ‘c’ to the left of the colon are input arguments. The definition on the right operates on these input arguments to generate the output. Note that we don’t need the “return” statement. The expression to the right is evaluated and returned by default.

When to use Lambda function:

Keep in mind that lambda functions can only be used when that function can be written as an expression. They don’t support multi-statement functions or functions that don’t return a value.

Python Filter function

The “filter” function operates on a list and returns a subset of that list after applying the filtering rule.

output_list = filter(f, input_list)

The filtering rule is provided as an input argument to the function, often in the form of a lambda function. In our case, ‘f’ is that filtering rule. Pretty straightforward, right? Let’s consider a simple case where we want to get the list of all numbers that are greater than 5 from a given input list.

>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> output_list = filter(lambda x: x > 5, my_list)
>>> output_list
[6, 7, 8, 9]

As we can see, the first argument is a function defined by “lambda x: x > 5”. The second argument is the input list on which “filter” will operate to produce the output.

python Map Function

The “map” function transforms a given list into a new list by transforming each element using a rule.

output_list = map(f, input_list)

Let’s consider an example where we want to map each number of a list to its squared value.

>>> my_list = [1, 5, 7, 8, 11]
>>> output_list = map(lambda x: x * x, my_list)
>>> output_list
[1, 25, 49, 64, 121]

Another good thing about map() is that it can take multiple lists as input arguments. This is very useful when we do vector mathematics. The map() function will take multiple lists, operate on them, and then return an output list. One thing to note is that all the lists should be of the same size because map() applies the function to corresponding elements in those lists. Let’s say we want to element-wise add two lists:

>>> new_list = [-4, 3, 2, -6, 5]
>>> map(lambda x, y: x + y, my_list, new_list)
[-3, 8, 9, 2, 16]

Python Reduce Function

The “reduce” function will transform a given list into a single value by applying a given function continuously to all the elements. It basically keeps operating on pairs of elements until there are no more elements left. Let’s say we want to find the product of all the elements in a given list:

>>> my_list = [1, 5, 7, 8, 11]
>>> a = reduce(lambda a, b: a * b, my_list)
>>> print ('output ', a)
>>> output 3080

Combining all four (Lambda, Filter, Map and reduce)

Let’s flex our analytical muscles and consider an example where we can see all four operators in action. Let’s say you want to find the sum of squares of all the even numbers between 1 and 151. Instead of writing a big function, we can do it in a single line:

>>> my_sum = reduce(lambda a, b: a + b, map(lambda x: x * x, filter(lambda y: not y % 2, range(151))))
>>> my_sum

As we can see, the sum is 573800. Too complex? Okay let’s break it down. There are three lambda functions defined here:

f1 = lambda a, b: a + b
f2 = lambda x: x * x
f3 = lambda y: not y % 2

If we substitute these names, then we will get:

>>> reduce(f1, map(f2, filter(f3, range(151))))

Here, filter(f3, range(151)) outputs a list of all the even numbers between 1 and 151. Let’s call this “even_list”:

>>> reduce(f1, map(f2, even_list))

Now, map(f2, even_list) will compute the squares of all the elements in “even_list”. Let’s call this “squared_even_list”:

>>> reduce(f1, squared_even_list)

Finally, this function will compute the sum of all the elements in squared_even_list. This computing paradigm has been hugely influential in developing modern architectures and algorithms to operate on enormous amounts of data.