📉
Tutorials
  • Computer History
  • Function
    • Finance
      • Calculate
    • Manage Data
    • Date&Time
    • Strings and Character
  • Snippets
    • Web Application
      • Hugo
      • JavaScript
        • Stopwatch using JavaScript?
    • Note
    • Start Project
      • GitHub
      • GitLab
    • Python Programming
      • Strings and Character Data
      • List
      • Dictionaries
    • Data Science
      • Setting Option
      • Get Data
  • Link Center
    • Next Articles
    • Google
    • Excel VBA
    • Python
      • Notebook
    • WebApp
      • Vue.js
    • Finance
    • Project
      • Kids
        • Scratch
      • Finance
        • Plotly.js
        • Portfolio
      • Mini Lab
        • Systems Administration
        • Auto Adjust Image
      • Sending Emails
      • ECS
        • Knowledge Base
        • ระบบผู้เชี่ยวชาญ (Expert System)
        • Check product
        • Compare two SQL databases
      • e-Library
        • Knowledge base
        • การจัดหมวดหมู่ห้องสมุด
        • Temp
      • AppSheet
        • บัญชีรายรับรายจ่าย
      • Weather App
      • COVID-19
  • Tutorials
    • Data Science
      • Data Science IPython notebooks
    • UX & UI
      • 7 กฎการออกแบบ UI
    • Web Scraping
      • Scrape Wikipedia Articles
      • Quick Start
    • GUI
      • pysimple
        • Create a GUI
      • Tkinter
        • Python Tkinter Tutorial
      • PyQt
        • PyQt Tutorial
    • MachineLearning
      • การพัฒนา Chat Bot
      • AI ผู้ช่วยใหม่ในการทำ Customer Segmentation
      • Customer Segmentation
      • ตัดคำภาษาไทย ด้วย PyThaiNLP API
    • Excel & VBA
      • INDEX กับ MATCH
      • รวมสูตร Excel ปี 2020
      • How to Write Code in a Spreadsheet
    • Visualization
      • Bokeh
        • Part I: Getting Started
        • Data visualization
        • Plotting a Line Graph
        • Panel Document
        • Interactive Data Visualization
    • VueJS
      • VueJS - Quick Guide
    • Django
      • Customize the Django Admin
      • พัฒนาเว็บด้วย Django
    • Git
      • วิธีสร้าง SSH Key
      • Git คืออะไร
      • เริ่มต้นใช้งาน Git
      • การใช้งาน Git และ Github
      • รวม 10 คำสั่ง Git
      • GIT Push and Pull
    • Finance
      • Stock Analysis using Pandas (Series)
      • Building Investment AI for fintech
      • Resampling Time Series
      • Python for Finance (Series)
      • Stock Data Analysis (Second Edition)
      • Get Stock Data Using Python
      • Stock Price Trend Analysis
      • Calculate Stock Returns
      • Quantitative Trading
      • Backtrader for Backtesting
      • Binance Python API
      • Pine Script (TradingView)
      • Stocks Analysis with Pandas and Scikit-Learn
      • Yahoo Finance API
      • Sentiment Analysis
      • yfinance Library
      • Stock Data Analysis
      • YAHOO_FIN
      • Algorithmic Trading
    • JavaScript
      • Split a number
      • Callback Function
      • The Best JavaScript Examples
      • File and FileReader
      • JavaScript Tutorial
      • Build Reusable HTML Components
      • Developing JavaScript components
      • JavaScript - Quick Guide
      • JavaScript Style Guide()
      • Beginner's Handbook
      • Date Now
    • Frontend
      • HTML
        • File Path
      • Static Site Generators.
        • Creating a New Theme
    • Flask
      • Flask - Quick Guide
      • Flask Dashboards
        • Black Dashboard
        • Light Blue
        • Flask Dashboard Argon
      • Create Flask App
        • Creating First Application
        • Rendering Pages Using Jinja
      • Jinja Templates
        • Primer on Jinja Templating
        • Jinja Template Document
      • Learning Flask
        • Ep.1 Your first Flask app
        • Ep.2 Flask application structure
        • Ep.3 Serving HTML files
        • Ep.4 Serving static files
        • Ep.5 Jinja template inheritance
        • Ep.6 Jinja template design
        • Ep.7 Working with forms in Flask
        • Ep.8 Generating dynamic URLs in Flask
        • Ep.9 Working with JSON data
        • Ep.23 Deploying Flask to a VM
        • Ep.24 Flask and Docker
        • Ep. 25: uWSGI Introduction
        • Ep. 26 Flask before and after request
        • Ep. 27 uWSGI Decorators
        • Ep. 28 uWSGI Decorators
        • Ep. 29 Flask MethodView
        • Ep. 30 Application factory pattern
      • The Flask Mega-Tutorial
        • Chapter 2: Templates
      • Building Flask Apps
      • Practical Flask tutorial series
      • Compiling SCSS to CSS
      • Flask application structure
    • Database
      • READING FROM DATABASES
      • SQLite
        • Data Management
        • Fast subsets of large datasets
      • Pickle Module
        • How to Persist Objects
      • Python SQL Libraries
        • Create Python apps using SQL Server
    • Python
      • Python vs JavaScript
      • Python Pillow – Adjust Image
      • Python Library for Google Search
      • Python 3 - Quick Guide
      • Regular Expressions
        • Python Regular Expressions
        • Regular Expression (RegEx)
        • Validate ZIP Codes
        • Regular Expression Tutorial
      • Python Turtle
      • Python Beginner's Handbook
      • From Beginner to Pro
      • Standard Library
      • Datetime Tutorial
        • Manipulate Times, Dates, and Time Spans
      • Work With a PDF
      • geeksforgeeks.org
        • Python Tutorial
      • Class
      • Modules
        • Modules List
        • pickle Module
      • Working With Files
        • Open, Read, Append, and Other File Handling
        • File Manipulation
        • Reading & Writing to text files
      • Virtual Environments
        • Virtual Environments made easy
        • Virtual Environmen
        • A Primer
        • for Beginners
      • Functions
        • Function Guide
        • Inner Functions
      • Learning Python
        • Pt. 4 Python Strings
        • Pt. 3 Python Variables
      • Zip Function
      • Iterators
      • Try and Except
        • Exceptions: Introduction
        • Exceptions Handling
        • try and excep
        • Errors and Exceptions
        • Errors & Exceptions
      • Control Flow
      • Lambda Functions
        • Lambda Expression คืออะไร
        • map() Function
      • Date and Time
        • Python datetime()
        • Get Current Date and Time
        • datetime in Python
      • Awesome Python
      • Dictionary
        • Dictionary Comprehension
        • ALL ABOUT DICTIONARIES
        • DefaultDict Type for Handling Missing Keys
        • The Definitive Guide
        • Why Functions Modify Lists and Dictionaries
      • Python Structures
      • Variable & Data Types
      • List
        • Lists Explained
        • List Comprehensions
          • Python List Comprehension
          • List Comprehensions in 5-minutes
          • List Comprehension
        • Python List
      • String
        • Strings and Character Data
        • Splitting, Concatenating, and Joining Strings
      • String Formatting
        • Improved String Formatting Syntax
        • String Formatting Best Practices
        • Remove Space
        • Add Spaces
      • Important basic syntax
      • List all the packages
      • comment
    • Pandas
      • Tutorial (GeeksforGeeks)
      • 10 minutes to pandas
      • Options and settings
      • เริ่มต้น Set Up Kaggle.com
      • Pandas - Quick Guide
      • Cookbook
      • NumPy
        • NumPy Package for Scientific
      • IO tools (text, CSV, …)
      • pandas.concat
      • Excel & Google Sheets
        • A Guide to Excel
        • Quickstart to the Google Sheets
        • Python Excel Tutorial: The Definitive Guide
      • Working With Text Data
        • Quickstart
      • API Reference
      • Groupby
      • DateTime Methods
      • DataFrame
      • sort_values()
      • Pundit: Accessing Data in DataFrames
      • datatable
        • DataFrame: to_json()
        • pydatatable
      • Read and Write Files
      • Data Analysis with Pandas
      • Pandas and Python: Top 10
      • 10 minutes to pandas
      • Getting Started with Pandas in Python
    • Markdown
      • Create Responsive HTML Emails
      • Using Markup Languages with Hugo
    • AngularJS
      • Learn AngularJS
    • CSS
      • The CSS Handbook
      • Box Shadow
      • Image Center
      • The CSS Handbook
      • The CSS Handbook
      • Loading Animation
      • CSS Grid Layout
      • Background Image Size
      • Flexbox
  • Series
    • จาวาสคริปต์เบื้องต้น
      • 1: รู้จักกับจาวาสคริปต์
  • Articles
    • Visualization
      • Dash
        • Introducing Dash
    • Finance
      • PyPortfolioOpt
      • Best Libraries for Finance
      • Detection of price support
      • Portfolio Optimization
      • Python Packages For Finance
    • Django
      • เริ่มต้น Django RestFramework
    • General
      • Heroku คืออะไร
      • How to Crack Passwords
    • Notebook
      • IPython Documentation
      • Importing Notebooks
      • Google Colab for Data Analytics
      • Creating Interactive Dashboards
      • The Definitive Guide
      • A gallery of interesting Jupyter Notebooks
      • Advanced Jupyter Notebooks
      • Converting HTML to Notebook
    • Pandas
      • Pandas_UI
      • Pandas Style API
      • Difference Between two Dataframes
      • 19 Essential Snippets in Pandas
      • Time Series Analysis
      • Selecting Columns in a DataFrame
      • Cleaning Up Currency Data
      • Combine Multiple Excel Worksheets
      • Stylin’ with Pandas
      • Pythonic Data Cleaning
      • Make Excel Faster
      • Reading Excel (xlsx) Files
      • How to use iloc and loc for Indexing
      • The Easiest Data Cleaning Method
    • Python
      • pip install package
      • Automating your daily tasks
      • Convert Speech to Text
      • Tutorial, Project Ideas, and Tips
      • Image Handling and Processing
        • Image Processing Part I
        • Image Processing Part II
        • Image tutorial
        • Image Processing with Numpy
        • Converts PIL Image to Numpy Array
      • Convert Dictionary To JSON
      • JSON Dump
      • Speech-to-Text Model
      • Convert Text to Speech
      • Tips & Tricks
        • Fundamentals for Data Science
        • Best Python Code Examples
        • Top 50 Tips & Tricks
        • 11 Beginner Tips
        • 10 Tips & Tricks
      • Password hashing
      • psutil
      • Lambda Expressions
    • Web Scraping
      • Web Scraping using Python
      • Build a Web Scraper
      • Web Scraping for beginner
      • Beautiful Soup
      • Scrape Websites
      • Python Web Scraping
        • Web Scraping Part 1
        • Web Scraping Part 2
        • Web Scraping Part 3
        • Web Scraping Part 4
      • Web Scraper
    • Frontend
      • Book Online with GitBook
      • Progressive Web App คืออะไร
      • self-host a Hugo web app
  • Examples
    • Django
      • Build a Portfolio App
      • SchoolManagement
    • Flask
      • Flask Stock Visualizer
      • Flask by Example
      • Building Flask Apps
      • Flask 101
    • OpenCV
      • Build a Celebrity Look-Alike
      • Face Detection-OpenCV
    • Python
      • Make Game FLASH CARD
      • Sending emails using Google
      • ตรวจหาภาพซ้ำด้วย Perceptual hashing
        • Sending Emails in Python
      • Deck of Cards
      • Extract Wikipedia Data
      • Convert Python File to EXE
      • Business Machine Learning
      • python-business-analytics
      • Simple Blackjack Game
      • Python Turtle Clock
      • Countdown
      • 3D Animation : Moon Phases
      • Defragmentation Algorithm
      • PDF File
        • จัดการข้อความ และรูป จากไฟล์ PDF ด้วย PDFBox
      • Reading and Generating QR codes
      • Generating Password
        • generate one-time password (OTP)
        • Random Password Generator
        • Generating Strong Password
      • PyQt: Building Calculator
      • List Files in a Directory
      • [Project] qID – โปรแกรมแต่งรูปง่ายๆ เพื่อการอัพลงเว็บ
      • Python and Google Docs to Build Books
      • Tools for Record Linking
      • Create Responsive HTML Email
      • psutil()
      • Transfer Learning for Deep Learning
      • ดึงข้อมูลคุณภาพอากาศประเทศไทย
        • Image Classification
    • Web Scraper
      • Scrape Wikipedia Articles
        • Untitled
      • How Scrape Websites with Python 3
    • Finance
      • Algorithmic Trading for Beginners
      • Parse TradingView Stock
      • Creating a stock price database with MariaDB and python
      • Source Code
        • stocks-list
      • Visualizing with D3
      • Real Time Stock in Excel using Python
      • Create Stock Quote Module
      • The Magic Formula Lost Its Sparkle?
      • Stock Market Analysis
      • Stock Portfolio Analyses Part 1
      • Stock Portfolio Analyses Part 2
      • Build A Dashboard In Python
      • Stock Market Predictions with LSTM
      • Trading example
      • Algorithmic Trading Strategies
      • DOWNLOAD FUNDAMENTALS DATA
      • Algorithmic Trading
      • numfin
      • Financial Machine Learning
      • Algorithm To Predict Stock Direction
      • Interactive Brokers API Code
      • The (Artificially) Intelligent Investor
      • Create Auto-Updating Excel of Stock Market
      • Stock Market Predictions
      • Automate Your Stock Portfolio
      • create an analytics dashboard
      • Bitcoin Price Notifications
      • Portfolio Management
    • WebApp
      • CSS
        • The Best CSS Examples
      • JavaScript
        • Memory Game
      • School Clock
      • Frontend Tutorials & Example
      • Side Menu Bar with sub-menu
      • Create Simple CPU Monitor App
      • Vue.js building a converter app
      • jQuery
        • The Best jQuery Examples
      • Image Slideshow
      • Handle Timezones
      • Text to Speech with Javascript
      • Building Blog for Your Portfolio
      • Responsive Website Layout
      • Maths Homework Generator
  • Books
    • Finance
      • Python for Finance (O'Reilly)
    • Website
      • Hugo
        • Go Bootcamp
        • Hugo in Action.
          • About this MEAP
          • Welcome
          • 1. The JAM stack with Hugo
          • 2. Live in 30 minutes
          • 3. Using Markup for content
          • 4. Content Management with Hugo
          • 5. Custom Pages and Customized Content
          • 6. Structuring web pages
          • A Appendix A.
          • B Appendix B.
          • C Appendix C.
    • Python
      • ภาษาไพธอนเบื้องต้น
      • Python Cheatsheet
        • Python Cheatsheet
      • Beginning Python
      • IPython Cookbook
      • The Quick Python Book
        • Case study
        • Part 1. Starting out
          • 1. About Python
          • 2. Getting started
          • 3. The Quick Python overview
        • Part 2. The essentials
          • 14. Exceptions
          • 13. Reading and writing files
          • 12. Using the filesystem
          • 11. Python programs
          • 10. Modules and scoping rules
          • 9. Functions
          • 8. Control flow
          • 4. The absolute basics
          • 5. Lists, tuples, and sets
          • 6. Strings
          • 7. Dictionaries
        • Part 3. Advanced language features
          • 19. Using Python libraries
          • 18. Packages
          • 17. Data types as objects
          • 16. Regular expressions
          • 15. Classes and OOP
        • Part 4. Working with data
          • Appendix B. Exercise answers
          • Appendix A. Python’s documentation
          • 24. Exploring data
          • 23. Saving data
          • 20. Basic file wrangling
          • 21. Processing data files
          • 22. Data over the network
      • The Hitchhiker’s Guide to Python
      • A Whirlwind Tour of Python
        • 9. Defining Functions
      • Automate the Boring Stuff
        • 4. Lists
        • 5. Dictionaries
        • 12. Web Scraping
        • 13. Excel
        • 14. Google Sheets
        • 15. PDF and Word
        • 16. CSV and JSON
    • IPython
    • Pandas
      • จัดการข้อมูลด้วย pandas เบื้องต้น
      • Pandas Tutorial
  • Link Center
    • Temp
  • เทควันโด
    • รวมเทคนิค
    • Help and Documentation
  • Image
    • Logistics
Powered by GitBook
On this page
  • 1. Python Iterables tricks‌
  • ‌2. Python branching tricks
  • 3. Python comprehensions tricks
  • 4. Python unpacking tricks
  • 5. Python Itertools tricks
  • 6. Python collections tricks
  • 7. Other Python tricks
  • 8. Python easter eggs
  • Summary

Was this helpful?

  1. Articles
  2. Python
  3. Tips & Tricks

Top 50 Tips & Tricks

PreviousBest Python Code ExamplesNext11 Beginner Tips

Last updated 5 years ago

Was this helpful?

​‌

Here is a list of python tips and tricks to help you write an elegant Python 3 code! This article is divided into different kinds of tricks:‌

  • Python iterable tricks.

  • Python comprehension tricks.

  • Python unpacking tricks.

  • Python itertools tricks.

  • Python collections tricks.

  • Python other tricks.

  • Python easter eggs.

  • Python tricks to understand the context.

‌

1. Python Iterables tricks‌

Creating a sequence of numbers (zero to ten with skips).

range(0,10,2)​

# [0, 2, 4, 6, 8]                   

‌Summing a sequence of numbers (calculating the sum of zero to ten with skips).

l = range(0,10,2)sum(l)

​# 20

‌Checking whether any element in the sequence is Truthful (checking whether any elements between zero and ten with skips are even).

any(a % 2==0 for a in range(0,10,2))​

# True

‌Checking whether all elements in the sequence are Truthful (checking whether all elements between zero and ten with skips are even).

all(a % 2==0 for a in range(0,10,2))​

# True

‌Cumulative summing a sequence of numbers (calculating the cumulative sum of zero to ten with skips).

import numpy as np
res = list(np.cumsum(range(0,10,2)))
res​

# [ 0,  2,  6, 12, 20]

‌Given each iterable we construct a tuple by adding an index.

a = ['Hello', 'world', '!']
list(enumerate(a))​

# [(0, 'Hello'), (1, 'world'), (2, '!')]

‌Concatenating iterable to a single string.

a = ["python","really", "rocks"]
" ".join(a)

# 'python really rocks'

‌

Combining two iterable of tuples or pivot nested iterables.

# Combining two iterables
a = [1, 2, 3]
b = ['a', 'b', 'c']
z = zip(a, b)
z

# [(1, 'a'), (2, 'b'), (3, 'c')]

# Pivoting list of tuples
zip(*z)

# [(1, 2, 3), ('a', 'b', 'c')]

‌

Getting min/max from iterable (with/without specific function).

# Getting maximum from iterable
>>> a = [1, 2, -3]
>>> max(a)
# 2

# Getting maximum from iterable
>>> min(a)
# 1

# Bot min/max has key value to allow to get maximum by appliing function
>>> max(a,key=abs)
# 3

‌Getting sorted iterable (can sort by “compare” function).

>>> a = [1, 2, -3]
>>> sorted(a)
# [-3, 1, 2]

>>> sorted(a,key=abs)
# [1, 2, -3]

‌Splitting a single string to list.

>>> s = "a,b,c"
>>> s.split(",")

# ["a", "b", "c"]

‌

Initializing a list filled with some repetitive number.

[1]* 10

# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

‌

Merging/Upserting two dictionaries.

>>> a = {"a":1, "b":1}
>>> b = {"b":2, "c":1}
>>> a.update(b)
>>> a

# {"a":1, "b":2, "c":1}

‌

Naming and saving slices of iterables.

# Naming slices (slice(start, end, step))
>>> a = [0, 1, 2, 3, 4, 5]
>>> LASTTHREE = slice(-3, None)
>>> LASTTHREE

# slice(-3, None, None)

>>> a[LASTTHREE]
# [3, 4, 5]‌

Finding the index of an item in a list.

a = ["foo", "bar", "baz"]
a.index("bar")

# 1‌

Finding the index of the min/max item in an iterable.

a = [2, 3, 1]
min(enumerate(a),key=lambda x: x[1])[0]

# 2

‌

Rotating iterable by k elements.

>>> a = [1, 2, 3, 4]
>>> k = 2
>>> a[-2:] + a[:-2]​

# [3, 4, 1, 2]‌

Removing useless characters on the end/start/both of your string.

>>> name = "//George//"
>>> name.strip("/")
#'George'
>>> name.rstrip("/")
#'//George'
>>> name.lstrip("/")
#'George//'

‌

Reversing an iterable wit order (string, list etc).

# Reversing string
>>> s = "abc"
>>> s[::-1]

# "cba"

# Reversing list
>>> l = ["a", "b", "c"]
>>> l[::-1]

#["c", "b", "a"]

‌2. Python branching tricks

Multiple predicates short-cut.

n = 10
1 < n < 20

# True

For-else construct useful when searched for something and find it.

# For example assume that I need to search through a list and process each item until a flag item is found and 
# then stop processing. If the flag item is missing then an exception needs to be raised.

for i in mylist:
    if i == theflag:
        break
    process(i)
else:
    raise ValueError("List argument missing terminal flag.")

Trenary operator.

"Python ROCK" if True else " I AM GRUMPY"

# "Python ROCK"

Try-catch-else construct.

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")

While-else construct.

i = 5

while i > 1:
    print("Whil-ing away!")
    i -= 1
    if i == 3:
        break
else:
    print("Finished up!")

3. Python comprehensions tricks

List comprehension.

>>> m = [x ** 2 for x in range(5)]
>>> m

# [0, 1, 4, 9, 16]

Set comprehension.

>>> m = {x ** 2 for x in range(5)}
>>> m

# {0, 1, 4, 9, 16}

Dict comprehension.

m = {x: x ** 2 for x in range(5)}
m
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Generator comprehension.

# A generator comprehension is the lazy version of a list comprehension.
>>> m = (x ** 2 for x in range(5))
>>> m
# <generator object <genexpr> at 0x108efe408>
>>> list(m)
# [0, 1, 4, 9, 16]

>>> m = (x ** 2 for x in range(5))
>>> next(m)
# 0
>>> list(m)
# [1, 4, 9, 16]

List comprehension with the current and previous value.

>>> a = [1, 2, 4,2]
>>> [y - x for x,y in zip(a,a[1:])]
# [1, 2, -2]

Note: all comprehension can use predicates with if statement.

4. Python unpacking tricks

Unpack variables from iterable.

# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
# (1, 2, 3)

>>> a, b, c = [1, 2, 3]
>>> a, b, c
# (1, 2, 3)

Swap variables values.

>>> a, b = 1, 2
>>> a, b = b, a
>>> a, b
# (2, 1)

Unpack variables from iterable without indicating all elements.

>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
# 1
>>> b
# [2, 3, 4]
>>> c
# 5

Unpack variables using the splat operator.

>>> def test(x, y, z):
>>> 	print(x, y, z)  
>>> res = test(*[10, 20, 30]) 
# 10 20 30
>>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
# 10 20 30  

5. Python Itertools tricks

Flatten iterables.

>>> a = [[1, 2], [3, 4], [5, 6]]
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]

Creating cartesian products from iterables.

>>> for p in itertools.product([1, 2, 3], [4, 5]):
>>>    print(''.join(str(x) for x in p))

(1, 4)
(1, 5)
(2, 4)
(2, 5)
(3, 4)
(3, 5)

Creating permutation from iterable.

for p in itertools.permutations([1, 2, 3, 4]):
print(''.join(str(x) for x in p))
# 123
# 132
# 213
# 231
# 312
# 321

Creating ngram from iterable.

>>> from itertools import islice
>>> def n_grams(a, n):
...     z = (islice(a, i, None) for i in range(n))
...     return zip(*z)
...
>>> a = [1, 2, 3, 4, 5, 6]
>>> n_grams(a, 3)
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
>>> n_grams(a, 2)
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
>>> n_grams(a, 4)
[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]

Combining two iterables of tuples with padding or pivot nested iterable with padding.

>>> import itertools as it
>>> x = [1, 2, 3, 4, 5]
>>> y = ['a', 'b', 'c']
>>> list(zip(x, y))
[(1, 'a'), (2, 'b'), (3, 'c')]

>>> list(it.zip_longest(x, y))
# [(1, 'a'), (2, 'b'), (3, 'c'), (4, None), (5, None)]

Creating a combination of k things from an iterable of n

>>> import itertools
>>> bills = [20, 20, 20, 10, 10, 10, 10, 10, 5, 5, 1, 1, 1, 1, 1]
>>> list(itertools.combinations(bills, 3))
# [(20, 20, 20), (20, 20, 10), (20, 20, 10), ... ]

Creating accumulated results of iterable given a function

>>> import itertools
>>> list(itertools.accumulate([9, 21, 17, 5, 11, 12, 2, 6], min))

# [9, 9, 9, 5, 5, 5, 2, 2]

Creating an iterator that returns elements from the iterable as long as the predicate is true

>>> import itertools
>>> itertools.takewhile(lambda x: x < 3, [0, 1, 2, 3, 4])  
# [0, 1, 2]

>>> it.dropwhile(lambda x: x < 3, [0, 1, 2, 3, 4])
# [3, 4]

Creating an iterator that filters elements from iterable returning only those for which the predicate is _False_

>>> import itertools
# keeping only false values
>>> list(itertools.filterfalse(bool, [None, False, 1, 0, 10]))

# [None, False, 0]

Creating an iterator that computes the function using arguments obtained from the iterable of iterables

>>> import itertools 
>>> import operator
>>> a = [(2, 6), (8, 4), (7, 3)]
>>> list(itertools.starmap(operator.mul, a))

# [12, 32, 21]

6. Python collections tricks

Set basic operations.

>>> A = {1, 2, 3, 3}
>>> A
# set([1, 2, 3])
>>> B = {3, 4, 5, 6, 7}
>>> B
# set([3, 4, 5, 6, 7])
>>> A | B
# set([1, 2, 3, 4, 5, 6, 7])
>>> A & B
# set([3])
>>> A - B
# set([1, 2])
>>> B - A
# set([4, 5, 6, 7])
>>> A ^ B
# set([1, 2, 4, 5, 6, 7])
>>> (A ^ B) == ((A - B) | (B - A))
# True

Counter data structure (an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values).

import collections

>>> A = collections.Counter([1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7])
>>> A
# Counter({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1})
>>> A.most_common(1)
# [(3, 4)]
>>> A.most_common(3)
# [(3, 4), (1, 2), (2, 2)]

Default dictionary structure (a subclass of dictionary that retrieves default value when non-existing key getting accessed).

>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
# 0

>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
# 'a'

>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
# '[default value]'

>>> m = collections.defaultdict(list)
>>> m['a']
# []

Ordered dict structure (a subclass of dictionary that keeps order).

>>> from collections import OrderedDict

>>> d = OrderedDict.fromkeys('abcde')
>>> d.move_to_end('b')
>>> ''.join(d.keys())
# 'acdeb'

>>> d.move_to_end('b', last=False)
>>> ''.join(d.keys())
# 'bacde'

Deques structure (Deques are a generalization of stacks and queues).

>>> import collection
>>> Q = collections.deque()
>>> Q.append(1)
>>> Q.appendleft(2)
>>> Q.extend([3, 4])
>>> Q.extendleft([5, 6])
>>> Q
deque([6, 5, 2, 1, 3, 4])
>>> Q.pop()
4
>>> Q.popleft()
6
>>> Q
deque([5, 2, 1, 3])
>>> Q.rotate(3)
>>> Q
deque([2, 1, 3, 5])
>>> Q.rotate(-3)
>>> Q
deque([5, 2, 1, 3])

>>> last_three = collections.deque(maxlen=3)
>>> for i in range(4):
...     last_three.append(i)
...     print ', '.join(str(x) for x in last_three)
...
# 0
# 0, 1
# 0, 1, 2
# 1, 2, 3
# 2, 3, 4

Named tuples structure (create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable).

>>> import collections
>>> Point = collections.namedtuple('Point', ['x', 'y'])
>>> p = Point(x=1.0, y=2.0)
>>> p
Point(x=1.0, y=2.0)
>>> p.x
# 1.0
>>> p.y
# 2.0

Use A Dictionary To Store A Switch.

>>> func_dict = {'sum': lambda x, y: x + y, 'subtract': lambda x, y: x - y}
>>> func_dict['sum'](9,3)
# 12
>>> func_dict['subtract'](9,3)
# 6

Data classes structure

>>> from dataclasses import dataclass

>>> @dataclass
>>> class DataClassCard:
>>>    rank: str
>>>    suit: str
    
>>> queen_of_hearts = DataClassCard('Q', 'Hearts')
>>> queen_of_hearts.rank
# 'Q'
>>> queen_of_hearts
DataClassCard(rank='Q', suit='Hearts')
>>> queen_of_hearts == DataClassCard('Q', 'Hearts')
# True

7. Other Python tricks

Generating uuid.

# This creates a randomized 128-bit number that will almost certainly be unique.
# In fact, there are over 2¹²² possible UUIDs that can be generated. That’s over five undecillion (or 5,000,000,000,000,000,000,000,000,000,000,000,000).

>>> import uuid
>>> user_id = uuid.uuid4()
>>> user_id 

# UUID('7c2faedd-805a-478e-bd6a-7b26210425c7')

Memoization using LRU cache.

import functools

@functools.lru_cache(maxsize=128)
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    return fibonacci(n - 1) + fibonacci(n - 2)

Suppression of expressions

>>> from contextlib import suppress
>>> with contextlib.suppress(ZeroDivisionError):
>>>     10/0
# No exception raised

An elegant way to deal with a file path (3.4≥)

>>> from pathlib import Path
>>> data_folder = Path("source_data/text_files/")

# Path calculation and metadata
>>> file_to_open = data_folder / "raw_data.txt"
>>> file_to_open.name
# "raw_data.txt"
>>> file_to_open.suffix
# "txt"
>>>file_to_open.stem
# "raw_data"
                       
# Files functions                       
>>> f = open(file_to_open)
>>> f.read()
# content of the file                      
>>> file_to_open.exists()

# True

Creating decorator to separate concerns

>>>from functools import wraps

>>>def add_sandwich(wrapped):
>>>    @wraps(wrapped)
>>>    def wrapper(*args, **kwargs):
>>>        return wrapped(*args, **kwargs) + ' sandwich'
>>>    return wrapper

>>>@add_sandwich
>>>def ham():
>>>    return 'ham'

>>>ham()

# 'ham sandwich'

Using yield to create a simple iterator

>>> def foo(lst):
>>>   for x in lst:
>>>       yield x
>>>       yield x*2

>>> a = [1, 3]
>>> list(foo(a))

# [1, 2, 3, 6]

8. Python easter eggs

Anti-gravity

import antigravity

antigravity.fly()

The Zen of Python

>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one and preferably only one obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea let's do more of those!

List object attributes

dir()
# ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

dir("Hello World")
# ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Summary

_If you think I should add any more or have suggestions please do let me know in the comments. Thank for reading !

​​

Source :