Python Tips and Tricks for Competitive Programming

Python Programming language makes everything easier and straightforward. Effective use of its built-in libraries can save a lot of time and help with faster submissions while doing Competitive Programming. Below are few such useful tricks that every Pythonist should have at their fingertips:
Below is the implementation to convert a given number into a list of digits:
Python3
# Python program to convert a# number to a list of digitsÂ
# Given numbern = 123456Â
# Stores the list of digitslis = list(map(int, str(n)))Â
# Print the digitsprint(lis) |
[1, 2, 3, 4, 5, 6]
Â
- Converting a sentence into a List of words using split() Function: Below is the implementation to convert a sentence into a list of words:
Python3
# Python program to convert# a sentence to a list of wordsÂ
# Given sentencesentence = "zambiatek is the computer science portal for zambiatek"Â
# Convert the sentence# into a list of wordslis = list(sentence.split())Â
# Print the list of wordsprint(lis) |
['zambiatek', 'is', 'the', 'computer', 'science', 'portal', 'for', 'zambiatek']
Â
- Take newline-separated integers as a List: Newline-separated input from the console can be taken in the form of a List using List Comprehension. Below is the implementation to take input of newline-separated integers as a list:
Python3
# Python program to take# newline-separated input# in the form of a listÂ
# Given inputn = int(input())Â
lis = [int(input()) for _ in range(n)] |
- Calculating GCD/HCF of two numbers: Gcd of two numbers can be computed in python using a built-in function gcd() offered by the Python Math Module.
Below is the implementation to demonstrate gcd() function:Â
Python3
# Python program to demonstrate gcd() functionimport matha = 8b = 24Â
# Print the GCD of a and bprint(math.gcd(a, b)) |
8
Â
- Print permutations of array: All permutations of an array can be efficiently generated using built-in permutations() method from itertools package. This method takes a List as input and returns an object List of Tuples that contains all permutations.
Below is the implementation of the approach:
Python3
# Python program to print all# permutations using library functionfrom itertools import permutationsÂ
# Get all permutations of [1, 2, 3]perm = permutations([1, 2, 3])Â
# Print the obtained permutationsfor i in list(perm):Â Â Â Â print (i) |
(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)
Â
- Printing a string multiple times without Loop: Below is the implementation to print a string multiple times without loop using string multiplication technique:
Python3
# Python program to print# a string given number of timesÂ
# Given stringstr ="India"Â
# Print the string 2 timesprint(str * 2) |
IndiaIndia
Â
- To print a list with spaces without loop: A list can be printed without running the loop by using the * operator in Python.
Below is the implementation to print a list with spaces without loop:Â
Python3
# Python program to print a list with spaces without looplis = [1, 2, 3, 4]Â
# Printing list elements with spacesprint(*lis) |
1 2 3 4
Â
- Convert binary string to decimal: A binary string can be converted to its decimal equivalent using built-in int() function.
Below is the implementation of the above approach:Â
Python3
# Python program to convert a# binary string to its decimal# equivalent using int() functionÂ
# Given stringbinary = "1010"Â
# Print decimal equivalentprint(int(binary, 2)) |
10
- To print sorted list with spaces: Sorting any sequence is very easy in Python using a built-in method sorted() and using * symbol to print list with spaces. Sorted() sorts any sequence (list, tuple) and always returns a list with the elements in a sorted manner, without modifying the original sequence.
Below is the implementation to print a sorted list with spaces:Â
Python3
# Python program to print a sorted list with # spaces using sorted() functionlis = [6, 2, 7, 3, 4]Â
# Print the sorted sequenceprint(*sorted(lis)) |
2 3 4 6 7
Â
- To find common elements in two arrays: The common elements in 2 arrays/lists can be done in a much simpler way using sets. The intersection() function in Python gives the common elements in both arrays/lists.
Below is the implementation to demonstrate intersection() function:
Python3
# Python program to print common elements in# both the list using intersection() functionarray1 = [4, 5, 6, 7, 8]array2 = [3, 4, 5, 1, 72]Â
# Print the common elementsprint(set(array1).intersection(set(array2))) |
{4, 5}
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



