Amazing hacks of Python - GeeksforGeeks

27 Dec, 2021
Python is indeed one of the smart and most trending languages. Here are some cool hacks that make python superb among all other languages. 
List comprehensions: List comprehension is the best and efficient technique to get rid of writing unnecessary lines of code. Read Article to know more. 
Printing a list: The list is not printed according to the user’s requirement. They are always printed in unwanted square brackets and single quotes. But there is a trivial solution to print the list efficiently by using the string’s join method. The join method turns the list into a string by casting each item into a string and connecting them with the string that joins was called on. 
Python
# Declaring the list geekgeek = ['Geeks', 'Programming', 'Algorithm', 'Article']   # Directly printing the listprint ("Simple List:", geek)   # Printing the list by join methodprint ('List by using join method: %s' % ', ' .join(geek))   # Direct use of join methodprint ('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 Zip tricks
Transpose a matrix: You can Read Here about this.
Partition a list into N groups: We used iter() as an iterator over a sequence. 
Python3
# Declaring the list geekgeek = ['Sun', 'Flowers', 'Peoples', 'Animals', 'Day', 'Night'] partition = list(zip (*[iter(geek)] * 2))print (partition)
Output: 
[('Sun', 'Flowers'), ('Peoples', 'Animals'), ('Day', 'Night')]
Explanation: [iter(geek)] * 2 produces a list containing 2 items of geek[] list, i.e. a list of length 2. *arg unpacks a sequence into arguments for a function call. Therefore we are passing the same iterator 2 times to zip(). 
Printing more than one list’s items simultaneously 
Python
list1 = [1, 3, 5, 7]list2 = [2, 4, 6, 8] # Here zip() function takes two equal length list and merges them# together in pairsfor a, b in zip(list1,list2):    print (a, b)
Output: 
1 2
3 4
5 6
7 8
Take the string as input and convert it into the list: 
Python3
# Reads a string from input and type case them to int# after splitting to white-spaces formatted_list = list(map(int, input().split()))print(formatted_list)
Input:
2 4 5 6
Output:
[2, 4, 5, 6] 
Convert the list of list into a single list 
Python3
# import the itertoolsimport itertools # Declaring the list geekgeek = [[1, 2], [3, 4], [5, 6]] # chain.from_iterable() function returns the# elements of nested list# and iterate from first list# of iterable till the last# end of the list lst = list(itertools.chain.from_iterable(geek))print(lst)
Output: 
[1, 2, 3, 4, 5, 6]
Printing the repeated characters: The task is to print the pattern like this Geeeeekkkkss. So we can easily print this pattern without using it for a loop. 
Python
# + used for string concatenation# To repeat the character n times, just multiply n # with that character  print ("G" + "e"*5 + "k"*4 + "s"*2)
Output:
Geeeeekkkkss
Read More: 10 interesting facts about Python Reference: https://www.quora.com/What-are-some-cool-Python-tricks
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
punamsingh628700
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?