Reducing line spacing when coding is a useful technique when coding in competitive programming. In large-scale programming competitions such as hackathons or Google Kickstart, many students and coders face the problem of lack of time. If you are a coder who uses a programming language like this, Python
you have come to the right place. In this article, I will discuss some techniques and built-in functions in python that are helpful for coding in competitions or in daily life.
1. List comprehension
List comprehension is the best way to reduce lines of code when creating lists in python. It converts multiple lines of code into a single line of code. The syntax for using list comprehensions is
newlist = [expression for item in iterable if condition == True]
E.g:
# define the list of super heros
super_heros = ['Iron Man', 'Captain America', 'Super Man', 'Wonder Women']
# this is used to extract the marvels super hero from the super heros list
marvel = [marvel_hero for marvel_hero in super_heros if marvel_hero == 'Iron Man' or marvel_hero == 'Captain America']
# print the resulting marvel list
print(marvel)
2. Lambda function
Lambda function is a very useful method, it only needs one line of code to write the function, instead of writing so many lines. When you use these functions in another function, the real power of the lambda function comes into play. It is also called an anonymous function. Syntax for using lambda functions
lambda arguments : expression
E.g:
# define the lambda function
cube = lambda x: x**3
# print the result
print(cube(3)
3. Exchange variables
In competitive programming, exchange is the most common concept. In most data structures, exchange is also used. In python, the way to perform the exchange is much easier, and if you find any difficulty in performing the exchange, it will not cause confusion. I will show you two ways, namely exchange execution in python and other languages.
In languages like C and C++, perform exchange
int a = 10;
int b = 20;
int temp = a;
a = b;
b = temp;
In python, you can perform swap
a = 10
b = 10
a, b = b, a
4. Reverse the list
When performing competitive programming, use list reversal for different types of problems. Many students use for
loops to reverse the list, which increases the complexity of the program and sometimes causes confusion when debugging code. Python allows you to reverse a list with just one line of code, without using for
loops. E.g:
#define the list
number_list = [1, 2, 3, 4, 5, 6]
# reverse the list using slicing
number_list[::-1]
# print the list
print(number_list) #[6, 5, 4, 3, 2, 1]
5. Iterate the list
Whenever it comes to issues related to iteration, most of us will try to use a longer syntax. If the problem is large and contains, it sometimes takes a lot of time nexting for loops
. Python allows you to iterate through lists without using old-style syntax. E.g:
# define the list
car_list = ['Toyota', 'Maruti', 'BMW', 'Honda']
# iterate the car_list
for car in car_list:
# print each car from the list
print(car)