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)
3
>>> 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

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)
17

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
573800

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.

What is the REPL?

A read–eval–print loop (REPL), also known as an interactive toplevel or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise.

A Quick Summary of Python PEP 8 Programming standards

Indentation, line-length & code wrapping

  • Always use 4 spaces for indentation (don’t use tabs)
  • Write in ASCII in Python 2 and UTF-8 in Python 3
  • Max line-length: 72 characters (especially in comments)
  • Always indent wrapped code for readablility
# Good:
result = some_function_that_takes_arguments(
    'argument one,
    'argument two',
    'argument three'
)

# Bad:
result = some_function_that_takes_arguments(
'argument one,
'argument two', 'argument three')
result2 = some_function_that_takes_arguments('argument one', 'argument two', 'argument three')

Imports

  • Don’t use wildcards
  • Try to use absolute imports over relative ones
  • When using relative imports, be explicit (with .)
  • Don’t import multiple packages per line
# Good:
import os
import sys
from mypkg.sibling import example
from subprocess import Popen, PIPE # Acceptable
from .sibling import example # Acceptable

# Bad:
import os, sys # multiple packages
import sibling # local module without "."
from mypkg import * # wildcards

Whitespace and newlines

  • 2 blank lines before top-level function and class definitions
  • 1 blank line before class method definitions
  • Use blank lines in functions sparingly
  • Avoid extraneous whitespace
  • Don’t use whitespace to line up assignment operators (=, :)
  • Spaces around = for assignment
  • No spaces around = for default parameter values
  • Spaces around mathematical operators, but group them sensibly
  • Multiple statements on the same line are discouraged
# Good:
spam(ham[1], {eggs: 2})
if x == 4:
    print x, y
    x, y = y, x
dict['key'] = list[index]
y = 2
long_variable = 3
hypot2 = x*x + y*y
c = (a+b) * (a-b)
def complex(real, imag=0.0):
    return magic(r=real, i=imag)
do_one()
do_two()

# Bad
spam ( ham[ 1 ], { eggs: 2 } ) # spaces inside brackets
if x == 4 : print x , y ; x , y = y , x # inline statements, space before commas
dict ['key'] = list [index] # space before dictionary key
y             = 2 # Using spaces to line up assignment operators
long_variable = 3
hypot2 = x * x + y * y # Too much space around operators
c = (a + b) * (a - b) # Too much space around operators
def complex(real, imag = 0.0):
    return magic(r = real, i = imag) # Spaces in default values

Comments

  • Keep comments up to date – incorrect comments are worse than no comments
  • Write in whole sentences
  • Try to write in “Strunk & White” English
  • Use inline comments sparingly & avoid obvious comments
  • Each line of block comments should start with “# “
  • Paragraphs in block comments should be separated by a line with a single “#”
  • All public functions, classes and methods should have docstrings
  • Docstrings should start and end with """
  • Docstring one-liners can be all on the same line
  • In docstrings, list each argument on a separate line
  • Docstrings should have a blank line before the final """
def my_function():
    """ A one-line docstring """

def my_other_function(parameter=False):
    """
    A multiline docstring.

    Keyword arguments:
    parameter -- an example parameter (default False) 

    """

Naming conventions

  • Class names in CapWords
  • Method, function and variables names in lowercase_with_underscores
  • Private methods and properties start with __double_underscore
  • “Protected” methods and properties start with _single_underscore
  • If you need to use a reserved word, add a _ to the end (e.g. class_)
  • Always use self for the first argument to instance methods
  • Always use cls for the first argument to class methods
  • Never declare functions using lambda (f = lambda x: 2*x)
class MyClass:
    """ A purely illustrative class """

    __property = None

    def __init__(self, property_value):
        self.__property = property_value

    def get_property(self):
        """ A simple getter for "property" """

        return self.__property

    @classmethod
    def default(cls):
        instance = MyClass("default value")
        return instance

Source: https://www.python.org/dev/peps/pep-0008/#introduction

Quick methods to add python Intrepreter into windows system path variable

All three methods are tested and it is working in all versions of windows OS.

Method 1:

  1. Hold down windows key and press pause key.
  2. Click Advanced System Settings.
  3. Click Environment Variables.
  4. Append ;C:\python27 to the Path variable

Method 2:

Setting path at Windows

To add the Python directory to the path for a particular session in Windows −

In command prompt − type path %path%;C:\Python and press Enter.

Note − C:\Python is the path of the Python directory

Python Environment Variables

Here are important environment variables, which can be recognized by Python −

S.No. System Variable & Description
1 PYTHONPATH

It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.

2 PYTHONSTARTUP

It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.

3 PYTHONCASEOK

It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.

4 PYTHONHOME

It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.

Method 3:

  1. [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
  2. Click [New] under “System Variable”
  3. Variable Name: PY_HOME, Variable Value:C:\path\to\python\version enter image description here
  4. Click [OK]
  5. Locate the “Path” System variable and click [Edit]
  6. Add the following to the existing variable:

    %PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk; enter image description here

  7. Click [OK] to close all of the windows.

As a final sanity check open a command prompt and enter python. You should see

>python "type version"

If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.

Dictionary .get method usage in python

Description

The method get() returns a value for the given key. If key is not available then returns default value None.

Syntax

Following is the syntax for get() method −

dict.get(key, default=None)

Parameters

  • key — This is the Key to be searched in the dictionary.
  • default — This is the Value to be returned in case key does not exist.

Return Value

This method return a value for the given key. If key is not available, then returns default value None.

Example

The following example shows the usage of get() method.

#!/usr/bin/python

diction = {'Name': 'Kumar', 'Age': 12}

print "Value : %s" %  diction.get('Age')
print "Value : %s" %  diction.get('School', "Nothing")

When we run above program, it produces following result −

Value : 12
Value : Nothing