Amazing Python Hacks

Python Methods and Functions

Python is truly one of the smartest and most popular languages ​​out there. Here are some cool hacks that make Python superior to all other languages.

  1. Understanding Lists: Understanding Lists — the best and most efficient method to get rid of unnecessary lines of code. 

    # Geek list declaration

    geek = [ 'Geeks' , ' Programming' , 'Algorithm' , ' Article' ]

     
    # Direct print list

    print ( "Simple List: " , geek)

      
    # Print the list using join method

    print ( ' List by using join method:% s' % ',' . join (geek))

      
    # Direct use of join method

    print ( 'Direct apply the join method: ' , ( ", " . join (geek)) )

      Output:  Simple List : ['Geeks',' Programming', 'Algorithm',' Article'] List by using join method: Geeks, Programming, Algorithm, Article Direct apply the join method: Geeks, Programming, Algorithm, Article 

    Cool tricks n and lightning

  2. Transpose the matrix: You can read here about it.
  3. Split the list into N groups: we used iter () as a sequence iterator.

     
    # Geek list declaration

    geek = [ 'Sun' , 'Flowers' , ' Peoples' , 'Animals' , ' Day' , 'Night' ]

     
    # In python 2.7 just remove the list keyword

    partition = list ( zip ( * [ iter (geek)] * 2 ))

    print (partition)

      Output:  [('Sun',' Flowers'), ('Peoples',' Animals'), ('Day',' Night')] 

    Explanation : [iter (geek)] * 2 creates a list containing 2 items from the geek [] list, ie a list of length 2. * arg unpacks a sequence into arguments for a function call. Therefore, we pass the same iterator 2 times to zip ().

  4. Print multiple list items at the same time

    list1 = [ 1 , 3 , 5 , 7 ]

    list2 = [ 2 , 4 , 6 , 8 ]

     
    # Here the zip () function takes two s the same length and concatenates them
    # together in pairs

    for a, b in zip ( list1, list2):

    print (a, b)

      Output:  1 2 3 4 5 6 7 8 
  5. Take a string as input and convert it to a list:

    # In Python 2.7 replace input () with raw_input ()
    # Reads a string from input and casing it into int
    # after splitting into spaces

     

    formatted_list  = list ( map ( int , input (). split ()))

    print (formatted_list)

      Input:  2 4 5 6  Output:  [2, 4, 5, 6] 
  6. Convert list into one list

    # import itertools

    import itertools

     
    # Declaring geek list

    geek = [[  1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]]

     
    # chain. from_iterable () function returns nested list items
    # and repeat from first iterable list to last
    # end of list

     

    lst = list (itertools.chain.from_iterable (geek))

    print (lst)

      Outpu t:  [1, 2, 3, 4, 5, 6] 
  7. Printing repeated characters: The task is to print the template, like this Geeeeekkkkss. This way we can easily print this template without using a for loop.

    # + used to concatenate lines
    # To repeat a character n times, just multiply n
    # with this character

    print ( "G" + " e " * 5 + "k" * 4 + "s" * 2 )

      Output:  Geeeeekkkkss 

Details her: