Question Python list data structure has append() and extend() methods. Both methods add new items to the list. What is the difference between the two? Append vs extend list in Python Append: adds a new item to the end of the list. The item can be an object of any type. It could be a
Introduction In this post, we are going to discuss a rarely used Python feature. We are familiar with the regular for and while loops but using an else clause after a loop is not common. We can live without this language feature however it can be used to write elegant code. Let us take an
Welcome to a new Python code snippet. In this post, we are going to learn how to check whether a file or directory (ex. file path) exists using Python. A common use case is to check if a file exists before opening for reading or writing. Let us take some examples… Check file exists in
Introduction Given a directory path on the file system, the following Python code snippets retrieve a list of file names and total count using different methods. You may use the one that best suite your needs or find it more elegant. Let us get started… Using glob module In this example, we are going to
Set vs list in Python A better way to understand the key difference between sets and lists in Python is to understand the definition regardless of language or syntax. Generally speaking, a set is a mathematical concept which refers to a collection of distinct objects (in math they are called elements). Distinct means there are
Introduction This question is frequently asked on many technical websites. Before we clarify the key difference between the two, please note that xrange is a Python 2.x function. In other words, Python 2.x has both range and xrange. On the other hand, in Python 3.x there is only one function called range. So this question
Introduction Selection sort is a simple in-place (i.e. no auxiliary memory) comparison based sorting algorithm. It is an inefficient algorithm with O(n2) running time as it uses two nested loops. Here is a summary of steps how the algorithm works: Given an unsorted list Start with the first element on the left side Scan all
Introduction Insertion sort is a simple in place (i.e no need for an auxiliary memory) sorting algorithm. It is an efficient algorithm for small data sets, specially for lists that are partially sorted. For more information about how sorting works, you may refer to the following article For more in depth information about insertion sort
Problem Implement merge sort algorithm in Python. Solution For definition and explanation, You may refer to the following post. This post is just a rewrite in Python. Before jumping directly to the code, check out the following nice YouTube clip Recursive merge sort in python Take a look at the comments for explanation… Thanks for
Table of contents Introduction What is an iterator? Iterator example What is an iterable? Iterable example – separate class Iterable example – single class Python containers What are generators? Generator types Generator function example Generator comprehension example Generator advantages Summary References Introduction Today, we are going to discuss a little bit confusing topic in Python