Python is indeed one of the smart and most trending language. Here are some cool hacks that makes a python superb among all other languages.
- List comprehensions: List comprehension is best and efficient technique to get rid of writing unnecessary lines of code. Read Article to know more.
- Printing a list: List are not printed according the user requirement. They are always printed in unwanted square brackets and single quotes. But there is 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 join was called on.# 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)))chevron_rightfilter_noneOutput: Simple List: ['Geeks', 'Programming', 'Algorithm', 'Article'] List by using join method: Geeks, Programming, Algorithm, Article Direct apply the join method: Geeks, Programming, Algorithm, Article
- Transpose a matrix: You can Read Here about this.
- Partition a list into N groups: We used iter() as an iterator over a sequence.
# Declaring the list geekgeek=['Sun','Flowers','Peoples','Animals','Day','Night']# In python 2.7, just remove the list keywordpartition=list(zip(*[iter(geek)]*2))print(partition)chevron_rightfilter_noneOutput: [('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
list1=[1,3,5,7]list2=[2,4,6,8]# Here zip() function takes two equal length list and merges them# together in pairsfora, binzip(list1,list2):print(a, b)chevron_rightfilter_noneOutput: 1 2 3 4 5 6 7 8
- Take the string as input and convert it into list:
#In Python 2.7 replace input() to raw_input()# Reads a string from input and type case them to int# after splitting to white-spacesformatted_list=list(map(int,input().split()))print(formatted_list)chevron_rightfilter_noneInput: 2 4 5 6 Output: [2, 4, 5, 6]
- Convert list of list into single list
# import the itertoolsimportitertools# 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 listlst=list(itertools.chain.from_iterable(geek))print(lst)chevron_rightfilter_noneOutput: [1, 2, 3, 4, 5, 6]
- Printing the repeated characters: Task is to print the pattern like this Geeeeekkkkss. So we can easily print this pattern without using for loop.
# + used for string concatenation# To repeat the character n times, just multiply n# with that characterprint("G"+"e"*5+"k"*4+"s"*2)chevron_rightfilter_noneOutput: Geeeeekkkkss
Cool Zip tricks
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 contribute.geeksforgeeks.org or mail your article to contribute@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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
