Tag: Ways]
Python : Different ways to Iterate over a List in Reverse Order
In this article we will discuss different ways to Iterate over a python list in reverse order. Suppose we have a python list of strings i.e. Advertisements # List of string wordList = [‘hi’, ‘hello’, ‘this’, ‘that’, ‘is’, ‘of’] Now we want to iterate over this list in reverse order( from end to start ) …
Python : Different ways to Iterate over a List in Reverse OrderRead More
Python : 6 Different ways to create Dictionaries
In this article, we will discuss different ways to create dictionary objects in python. What is a dictionary? dictionary is an associative container that contains the items in key/value pairs. For example, we if want to track the words and their frequency count in an article like, Advertisements “Hello” occurs 7 times “hi” occurs 10 times …
Python Tuple: Different ways to create a tuple and Iterate over it
In this article we will discuss what is tuple, how its different from list, different ways to create a tuple and iterate over it. What is a tuple ? Tuple is a kind of heterogeneous sequential container that can store elements of different types. Tuples are immutable i.e once created then we can not change …
Python Tuple: Different ways to create a tuple and Iterate over itRead More
Read a file line by line in Python (5 Ways)
In this article we will discuss different ways to read a file line by line in Python. Suppose we have a file data.txt in same directory as our python script. Let’s see how to read it’s contents line by line. Solution for Small Files : Get list of all lines in file using readlines() First …
Python: Print items of a dictionary line by line (4 Ways)
In this article, we will discuss different ways to print line by line the contents of a dictionary or a nested dictionary in python. Subscribe Table Of Contents Print a dictionary line by line using for loop & dict.items() Print a dictionary line by line by iterating over keys Print a dictionary line by line …
Python: Print items of a dictionary line by line (4 Ways)Read More
Python: Three ways to check if a file is empty
In this article, we will discuss different ways to check if a file is empty i.e. its size is 0 using os.stat() or os.path.getsize() or by reading its first character. Check if a file is empty using os.stat() in Python Python provides a function to get the statistics about the file, Advertisements os.stat(path, *, dir_fd=None, …
6 ways to get the last element of a list in Python
In this article, we will discuss six different ways to get the last element of a list in python. Get last item of a list using negative indexing List in python supports negative indexing. So, if we have a list of size “S”, then to access the Nth element from last we can use the …
6 Ways to check if all values in Numpy Array are zero (in both 1D & 2D arrays) – Python
In this article we will discuss seven different ways to check if all values in a numpy array are 0. Then we will look how to find rows or columns with only zeros in a 2D array or matrix. Check if all values in a 1D Numpy Array are Zero First of all, we will …
6 Ways to check if all values in Numpy Array are zero (in both 1D & 2D arrays) – PythonRead More
7 Ways to add all elements of list to set in python
In this article we will discuss 7 different ways to add all elements of a list to the set in python. Suppose we have a list and a set i.e. Advertisements # Set of numbers sample_set = {11, 12, 13, 14} # List of numbers list_of_num = [10, 11, 12, 13, 14, 15, 16] Now …
7 Ways to add all elements of list to set in pythonRead More
Check if type of a variable is string in Python (3 Ways)
In this article we will discuss three different ways to check if the type of a variable is string or not in python. Check if type of a variable is string in python using isinstance() function Python provides a function to check the type of a variable i.e. Advertisements isinstance(object, classinfo) This function returns True …
Check if type of a variable is string in Python (3 Ways)Read More