Change language

Python tricks for concurrent coding

|

Python is one such programming language that makes everything simpler and clearer. Anyone who dabbles in Python for the sake of competitive coding becomes addicted to its many features. Here is a list of some interesting features that I found most useful in a competitive programming environment.

  1. The Most_common function of the Counter package.
    This is probably the most useful feature. which I have ever used and is always at heart when writing any Python code. This function parses the list / line and helps to return the top n entities in the list / line according to their number of occurrences in descending order, where n — this is the number given by the programmer. Individual entities are returned along with their number of instances in a tuple, which can be easily rephrased / typed as needed.

    # Code to find the top 3 element and their quantity
    # using most_common

    from collections import Counter

     

    arr = [ 1 , 3 , 4 , 1 , 2 , 1 , 1 , 3 , 4 , 3 , 5 , 1 , 2 , 5 , 3 , 4 , 5 ]

    counter = Counter (arr)

    top_three = counter.most_common ( 3 )

    print (top_three)

    Output:

     [(1, 5), (3, 4), (4, 3)] 

    The output tuple clearly states that 1 has occurred 5 times, 3 happened 4 times and 4 happened 3 times.

  2. N-largest / n-smallest function of the heapq package.
    This function helps to return the top n smallest / largest elements in any lists, and here again n — this is the number specified by the programmer.

    # Python code to find the 3 largest and 4 smallest
    # list items.

    import heapq

     

    grades = [ 110 , 25 , 38 , 49 , 20 , 95 , 33 , 87 , 80 , 90 ]

    print (heapq.nlargest ( 3 , grades))

    print (heapq.nsmallest ( 4 , grades))

    Output:

     [110, 95, 90] [20, 25, 33, 38] 

    The first line displays the 3 largest numbers in the list of grades ... Likewise, the second line of output outputs 4 of the smallest items present in the list evaluations. Another feature of this function is that it does not skip repetitions. So instead of n, if we put in the length of the array, we would get the entire sorted array !!

  3. Dictionary and the concept

    # Python code to demonstrate zip usage.

    import heapq

      

    stocks = {

    ’ Goog’ : 520.54 ,

    ’FB’ : 76.45 ,

      ’yhoo’ : 39.28 ,

      ’AMZN’ : 306.21 ,

    ’APPL’ : 99.76

    }

     

    zipped_1 = zip (stocks.values ​​(), stocks.keys ())

     
    # sort by value

    print ( sorted (zipped_1))

     

    zipped_2 = zip (stocks.keys (), stocks.values ​​())

    print ( sorted (zipped_2))

    # sort by key

    Output:

     [(39.28, ’yhoo’), (76.45,’ FB’), (99.76, ’APPL’), (306.21,’ AMZN’), (520.54, ’Goog’)] [(’ AMZN ’, 306.21), (’ APPL’, 99.76), (’FB’, 76.45), (’ Goog’, 520.54), (’yhoo’, 39.28)] 
  4. Map function.
    This function is a tricky little shortcut that allows us to implement a simple function in a list of values ​​in a very unconventional way . The following example will give a simple application of this functionality. The function takes as parameters the name of the function and the name of the list to which the function should be applied.

    # Python code to apply the function in the list

    income = [ 10 , 30 , 75 ]

     

    def double_money (dollars):

      return dollars * 2

     

    new_income = list ( map (double_money, income) )

    print (new_income)

    Output:

     [20, 60, 150] 

    Here we just implemented a simple function that multiplies each value in a list by two and returns it as a new list.

These functions may seem innocent individually, but will definitely come in handy in the ENVIRONMENT TIME CODING in the sense that they offer a lot of functionality in a VERY short amount of code. The functionality discussed has very specific applications and acts like SHORTCUT or CHEAT-SHEET in concurrent encoding. Having these useful tricks up your sleeve might just give someone the COMPETITIVE EDGE they were looking for!

This article is courtesy of Siddhant Bajaj . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically