Some cool yet advance and hidden features of Python

Python has really some great features which make some tasks easier to write from the coding perspective. Sometimes these features called as hidden features as these are either very unique to Python or not very well known.

for…else Loop

Yes, you saw it right, python has for…else loop if we use break statement inside for loop. If the for loop completes without invoking the break statement, then else block executed. Consider below example:
Search an item in a list: The traditional way:

a_list = [1,4,7,6,3,15,2,10]
found = False
for a_item in a_list:
   if(a_item == 9):
     found = True
     break

if(found):
    print("Number has been found")
else:
    print("Number is not present")

Above code can be rewritten using For…else loop:

a_list = [1,4,7,6,3,15,2,10]
for a_item in a_list:
   if(a_item == 9):
       print("Number has been found")
       break
else:
    print("Number is not present")

So the above block is more expressive than before.

Note: Above two code segments are for demo purpose only. Searching an item in a list is more easier in python:

a_list = [1,4,7,6,3,15,2,10]
if 6 in a_list:
   print("6 is present in the list")
else:
   print("6 is not present in the list")
#OR
print("6 is present in the list" if 6 in a_list else "6 is not present in the list")

Comparison Operator chaining

What do you think about below syntax:

x = 5
1 < x < 10 #True
1 < x < 4  #False

If you think the above statement will evaluate 1 < x first which is True, and then True < 10, which is also True then you are wrong. This is the beauty of Python. Above syntax is equivalent to below statement:

x = 5
1 < x and x < 10 #True

You can create this type of chain as long as you want. Less typing, more comprehensive.

Slicing / Extended Slicing

Slicing is a very popular and well-known mechanism to cut part of a list/dict or string (equivalent to substring). The syntax for the slice is:
a_list[start:end]
a_list[start:end:step] — Extended

start – start index – 0 based
end – end index – +1 based
step – jump step
Any of these indices can be negative. See examples below:

a_list = [1,4,7,6,3,15,2,10]
List Values1476315210
Index01234567
position12345678
# 2nd element to 5th element, i.e. index = 1 to 4
a_list[1:5]
[4, 7, 6, 3]

# Last element only
a_list[-1]
10

# 3rd element to 7th element with step 2
a_list[2:7:2]
[7, 3, 2]

# 3rd element to the end of the list
a_list[2:]
[7, 6, 3, 15, 2, 10]

# start to 5th element
a_list[:5]
[1, 4, 7, 6, 3]

# 7th element to 4th element backwards
a_list[6:2:-1]
[2, 15, 3, 6]

Multiple Assignment

We can assign multiple values into multiple variables in a single statement. See the below examples:

a, b, c = 1, 2, 3
# a = 1, b = 2, c = 3

This method can be used to pack and unpack list or dictionary. Very crispy way of doing the same:

a_list = [1,2,3]
a, b, c = a_list
# a = 1, b = 2, c = 3 | Unpacking the list into variables

Value Swapping

Above multiple assignment methods can be used to swap variable value. See below example:

a, b = 1, 2
# a = 1, b = 2

#SWAPPING
a, b = b, a
# a = 2, b = 1

Argument unpacking / Star operator

Above list unpacking fails if we pass the same in the function. We have star (*) operator to accomplish the same:

def test_func(x,y,z):
    print(x,y,z)

a_list = [1,2,3]
test_func(*a_list)

# 1 2 3

So the Star Operator helps to unpack the values into variables. We have double-star (**) operator to unpack dictionary.

def test_func(x,y,z):
    print(x,y,z)

a_dict = {'x':1,'y':2,'z':3}
test_func(*a_dict)
# x y z
test_func(**a_dict)
# 1 2 3

For the dictionaries, single star pass the keys and double star pass he values.

Yield Statement

We all know about the return statement in functions. Which returns the value(s) and ends the function execution. But the yield statement is used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. Any function that contains a yield keyword is termed as a generator. Hence, yield is what makes a generator. yield keyword in Python is less known off but has a greater utility which one can think of.

# generator to print even numbers 
def gen_even(a_list) : 
    for a_item in a_list: 
        if a_item % 2 == 0: 
            yield a_item
  
# initializing list  
a_list = [1,4,7,6,3,15,2,10]
  
# printing initial list 
print ("The original list is: " +  str(a_list)) 
  
# printing even numbers  
print ("The even numbers in list are: ", end = " ") 
for a_no in gen_even(a_list): 
    print (a_no)

Above program only prints the even number from the original list. Hence gen_even function create a iterator with even number only with the help of yield statement.

Python Comprehensions – List, Set, Dict, generator

Comprehensions in python is an easy way to create sequence data like list, set, dict or generator. This is the pythonic way of coding which has been referred as beauty of python.
Python supports 4 types of comprehensions:

  1. List Comprehensions
  2. Set Comprehensions
  3. Dictionary Comprehensions
  4. Generator Comprehensions
1. List Comprehensions

Consider below problem statements.
Given a list of numbers,
1. Print a list contains square of each number from the list. Eg: nums = [1,2,3,4,5,6], Output: [1,4,9,16,25,36]
2. Print a list contains only even numbers from an existing list. Eg: nums = [1,2,3,4,5,6], Output: [2,4,6]
3. Print a list contains 0 and 1 from existing list, 0 if number is odd, 1 if number is even. Eg: nums = [1,2,3,4,5,6], Output: [0,1,0,1,0,1]
Lets do it in normal way first:

nums = [1,2,3,4,5,6]

# 1
result = []
for n in nums:
    result.append(n**2)
print(result)

# 2
result = []
for n in nums:
    if n % 2 == 0:
        result.append(n)
print(result)

# 3
result = []
for n in nums:
    if n % 2 == 0:
        result.append(1)
    else:
        result.append(0)
print(result)

Pretty lengthy.. Now, lets do with List Comprehensions:

nums = [1,2,3,4,5,6]

# 1
result = [n**2 for n in nums]
print(result)

# 2 
result = [n for n in nums if n % 2 == 0]
print(result)

# 3
result = [1 if n % 2 == 0 else 0 for n in nums]
print(result)

Pretty simple, right?
Use for loop to iterate the list, use expression before for, use if after for, use if..else in between.
[expression for item in sequance]

2. Set Comprehensions

This is similar to List comprehensions, Only difference is we create set instead of list.

nums = [1,2,3,4,5,6]
result = {n**2 for n in nums}
print(result)
3. Dictionary Comprehensions

Similar to Set comprehensions, only we use key:value pair.

nums = [1,2,3,4,5,6]
result = {n:n**2 for n in nums}
print(result)
4. Generator Comprehensions

This one is same as others but we use round brackets instead of square brackets:
(expression for item in sequence)

Note: As we use () for this, do not misinterpret as tuple comprehensions because tuples are immutable hence cannot be generated this way.

Generator comprehensions returns a generator object that means, it computes only when it is required to generate an item. Hence it is memory efficient and lazy loading.

nums = [1,2,3,4,5]
result = (n**2 for n in nums)
print(type(result))                 # <class 'generator'>
for n in result:
    print(n,end=' ')                # 1 4 9 16 25
print(list(result))                 # [] print empty list as generator runs and allocate memory only once

Note:
1. You can access generator objects only once. Once you traverse an item in the generator object, it is gone from memory. So, even if it is memory efficient, it is not re-usable.
2. If you access partial objects from generator, then it can be time savings, as it does not computes the list together. No time savings for full traverse though.

Comprehensions beautify the codes, it can be efficient as well in many cases. For the efficiency, I will write a separate article to demonstrate when not to use comprehensions because some cases, comprehensions takes more time depending upon the operation/expression.

These were few advanced but less known features of python. Please note all the examples created in Python 3.x.

Happy Coding !!

1 thought on “Some cool yet advance and hidden features of Python”

  1. This is a great stuff to know and learn!! Can you post something on DS & Algo !! 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top