Python Useful One-Liners

Python is a popular high-level general-purpose programming language. Python programs are typically smaller than those written in other programming languages such as Java. Programmers must type relatively little, and the language’s indentation constraints ensure that their code is always readable. Python programs, on the other hand, can be made more concise by using one-liner codes. These can save time by requiring less coding to be typed.

Useful One-Liners in Python

1)Combining Two Dictionaries into one

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Give the other dictionary as static input and store it in another variable.
  • Combine the above given two dictionaries using the ‘|’ (or) Operator and store it in another variable.
  • Print the above-obtained combined dictionary.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict1= {'p': 30, 'q': 60, 'r': 90}
# Give the other dictionary as static input and store it in another variable.
gvn_dict2 = {'x': 20, 'f': 50, 'k': 80}
# Combine the above given two dictionaries using the '|' (or) Operator and 
# store it in another variable.
rslt_dict = gvn_dict1 | gvn_dict2
# Print the above-obtained combined dictionary.
print("The above-obtained combined dictionary is:\n", rslt_dict)

Output:

The above-obtained combined dictionary is:
{'p': 30, 'q': 60, 'r': 90, 'x': 20, 'f': 50, 'k': 80}

2)Find the most often occurring element.

Approach:

  • Import Counter function from collections module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Apply Counter function on the given list and store it in another variable.
  • Print the most frequently occurred element from the above counter list using the most_common() function.
  • The Exit of the Program.

Below is the implementation:

# Import Counter function from collections module using the import keyword
from collections import Counter
# Give the list as static input and store it in a variable.
l = ['5', 'p', 'q', '1', '1', 'r', 'p', '4', 'p', '7', '9']
# Apply Counter function on the given list and store it in another variable.
dict_lst = Counter(l)
# Print the most frequently occurred element from the above counter
# list using the most_common() function
print("The most often occured item from the given list is:")
print(dict_lst.most_common()[0][0])

Output:

The most often occured item from the given list is:
p

3)Obtain both the quotient and the remainder at the same time.

divmod():

divmod() returns a tuple and its functionality is derived from the combination of modulo percent and division/operators.

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given first and second numbers as arguments to the divmod() function to get the quotient and remainder at the same time.
  • Store it in two separate variables.
  • Print the quotient of the given two numbers.
  • Print the remainder of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

# Give the first number as static input and store it in a variable.
gvn_num1 = 525
# Give the second number as static input and store it in another variable.
gvn_num2 = 25
# Pass the given first and second numbers as arguments to the divmod() function
# to get the quotient and remainder at the same time.
# Store it in two separate variables
rslt_quotient, rslt_rem = divmod(gvn_num1, gvn_num2)
# Print the quotient of the given two numbers
print("The quotient of the given two numbers = ", rslt_quotient)
# Print the remainder of the given two numbers
print("The remainder of the given two numbers = ", rslt_rem)

Output:

The quotient of the given two numbers =  21
The remainder of the given two numbers =  0

4)Get the first n Fibonacci numbers

Approach:

  • Get the Fibonacci numbers using the lambda function.
  • Print the random Fibonacci number.
  • The Exit of the Program.

Below is the implementation:

# Getting the Fibonacci numbers using the lambda function
fibanoci_nums = lambda a: a if a <= 1 else fibanoci_nums(a - 1) + fibanoci_nums(a - 2)
# Printing the random Fibonacci number
print(fibanoci_nums(12))
print(fibanoci_nums(7))

Output:

144
13

5)Using set to Remove Duplicate elements from a List

Approach:

  • Give the list as static input and store it in a variable.
  • Print the given list.
  • Apply set() function on the given list to remove the duplicate elements and store it in another variable.
  • Print the list after the removal of duplicate elements.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 3, 4, 2, 3, 5, 5, 1]
# Print the given list
print("The given list = ", gvn_lst)
# Apply set() function on the given list to remove the duplicate elements and
# store it in another variable.
rslt_lst = set(gvn_lst)
# Print the list after removal of duplicate elements
print("The given list after removal of duplicate elements:\n", rslt_lst)

Output:

The given list =  [1, 2, 3, 4, 2, 3, 5, 5, 1]
The given list after removal of duplicate elements:
 {1, 2, 3, 4, 5}