Main menu


Python tricks: how to cut lines of code

Python tricks: how to cut lines of code

5 Python tricks you didn't know about how to cut lines of code

Reducing line spacing when coding is a useful technique when coding in competitive programming. In large programming competitions like hackathons or Google Kickstart, many students and coders face the problem of running out of time. If you're a coder using a programming language like this, you Python've come to the right place. This article describes some techniques and Python built-in functions that can help you with coding in competitions or in your daily life.

1. Understanding the list

List comprehensions are the best way to reduce lines of code when creating lists in Python. Converts multiple lines of code into a single line of code. The syntax for using list nesting is:

newlist = [expression for item in iterable if condition == True]

Yes :

# 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

2. Lambda function

Lambda functions are a very useful way to write a function with just one line of code instead of writing too many lines. The true power of lambda functions comes into play when you use these functions in other functions. Also called anonymous function. Syntax for using lambda functions

lambda arguments : expression

Yes :

# define the lambda function
cube = lambda x: x**3
# print the result

3. Exchange variable

In competitive programs, exchange is the most common concept. Exchange is also used in most data structures. In Python, the way to do the swap is much easier and doesn't cause any confusion if you have difficulty doing the swap. I'm going to show you two ways to do this: running the exchange in Python and another language.

Perform exchanges in languages ​​such as C and C++

int a = 10;
int b = 20;
int temp = a;
a = b;
b = temp;

In Python you can do swap.

a = 10
b = 10
a, b = b, a

4. Invert List

Use list inversion for different types of problems when doing competitive programming. Many students use loops forto flip the list, which increases the complexity of the program and sometimes creates confusion when debugging code. With Python, you can reverse a list in just one line of code without using forloops . Yes :

#define the list
number_list = [1, 2, 3, 4, 5, 6]
# reverse the list using slicing
# print the list
print(number_list) #[6, 5, 4, 3, 2, 1]

5. Repeat List

Whenever they run into problems with iteration, most people want to use the longer syntax. Sometimes it takes a lot of time if the problem is large and involved nexting for loops. Python allows you to iterate over a list without using old-style syntax. Yes :

# define the list
car_list = ['Toyota', 'Maruti', 'BMW', 'Honda']
# iterate the car_list
for car in car_list:
# print each car from the list