Python examples to to check the end of a string for specific text patterns e.g. domain name extenstions and so on. Use endswith() with strings A simple way to check the end a string is to use the String.endswith(). Use endswith() with tuples If you need to check against multiple choices, simply provide a tuple […]
Python examples to to check the start of a string for specific text patterns e.g. URL schemes and so on. Use startswith() with strings A simple way to check the beginning a string is to use the String.startswith(). Use startswith() with tuples If you need to check against multiple choices, simply provide a tuple of […]
Python example to split string into fields using the delimiters in the string. Split string using split() method Easiest way to split a string using a delimiter is using string.split( delimiter ) function. Split string with multiple delimiters The split() method of string objects is really meant for very simple cases, and does not allow […]
Python example to find common items between 2 or more dictionaries i.e. dictionary intersection items. Find common items using ‘&’ operator Simplest method is to use & operator between dictionaries. Use set.intersection() method Happy Learning !!
An OrderedDict maintains the insertion order of items added to dictionary. The order of items is preserved when iterating or serializing also. OrderedDict example To easily construct such dictionaries, you can use defaultdict in the collections module. A feature of defaultdict is that it automatically initializes the first value so you can simply focus on […]
‘multidict‘ word is used to refer a dictionary where mapping a single key to multiple values is possible. e.g. Multi dictionary example To easily construct such dictionaries, you can use defaultdict in the collections module. A feature of defaultdict is that it automatically initializes the first value so you can simply focus on adding items. […]
Priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served […]
Python examples to find the largest (or the smallest) item from a collection of comparable elements. max() This function is used to – compute the maximum of the values passed in its argument. lexicographically largest value if strings are passed as arguments. A little complex structure. min() This function is used to – compute the […]
Python examples to find the largest (or the smallest) N elements from a collection of elements. Using heapq module Python heapq module can be used to find N largest or smallest items from collections. It has two functions to help with – nlargest() nsmallest() Find items in simple iterables Find items in complicated iterables Happy […]
Python examples to unpack tuple or sequence or iterable but the iterable may be longer than N elements, causing a “too many values to unpack” exception. Unpack tuple with arbitrary length Python “star expressions” can be used to to unpack tuples with arbitrary length. Unpack and throw away unwanted values Sometimes you might want to […]
Python examples to unpack an N-element tuple or sequence into a collection of N variables. Unpack tuple examples Any sequence (or iterable) can be unpacked into variables using a simple assignment operation. The only requirement is that the number of variables and structure match the sequence. Possible Error If there is a mismatch in the […]
Learn to install python package in sublime editor for features like autocomplete and run build within sublime editor itself.
In this tutorial, we show how to work with the Python httplib2 module. The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. Python httplib2 module provides methods for accessing Web resources via HTTP. It supports many features, […]
Python language contains the csv module which has classes to read and write data in the CSV format. Let’s learn it using some examples.