Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Nizamuddin Siddiqui
1,958 articles
How to use for loop to print all the elements of a list in R?
**Issue:** This article is about R programming, not Python. Since you asked me to improve a Python article, I'll convert this to Python while maintaining the same teaching approach. In Python, you can use a for loop to iterate through all elements of a list. The syntax is straightforward: for item in list_name: where item represents each element in the list. Basic Syntax The basic syntax for iterating through a list in Python is ? for item in list_name: print(item) Example with Simple List Let's start with a ...
Read MoreHow to find the statistical summary of an R data frame with all the descriptive statistics?
When analyzing data in R, the default summary() function provides basic statistics like minimum, quartiles, mean, and maximum. However, for comprehensive statistical analysis, we need additional descriptive measures such as variance, standard deviation, skewness, and kurtosis. The basicStats() function from the fBasics package provides all these descriptive statistics in one output. Loading Required Package First, install and load the fBasics package ? library(fBasics) Example 1: mtcars Dataset Let's examine the built-in mtcars dataset ? data(mtcars) head(mtcars, 10) ...
Read MorePython - Write multiple files data to master file
When working with multiple data files, you often need to combine them into a single master file. Python provides several approaches to merge multiple files, from basic file operations to using pandas for structured data. Basic File Operations Approach This method reads multiple text files and writes their content to a master file using standard file operations ? import os # Create sample data files os.makedirs('data_files', exist_ok=True) # Create sample files with open('data_files/file1.txt', 'w') as f: f.write('John, Developer, 50000') f.write('Alice, Designer, 45000') with open('data_files/file2.txt', 'w') ...
Read MorePython - Working with .docx module
Word documents contain formatted text wrapped within three object levels. Lowest level − Run objects, Middle level − Paragraph objects and Highest level − Document object. So, we cannot work with these documents using normal text editors. But we can manipulate these word documents in Python using the python-docx module. Installation The first step is to install this third-party module python-docx. You can use pip ? pip install python-docx Important: After installation, import docx NOT python-docx. Use docx.Document class to start working with the word document. Creating a Basic Word Document ...
Read MorePython - Which is faster to initialize lists?
Python offers multiple ways to initialize lists, but their performance varies significantly. Understanding which method is fastest helps write more efficient code. This article compares four common list initialization methods: for loops, while loops, list comprehensions, and the star operator. Performance Comparison of List Initialization Methods Let's benchmark four different approaches to initialize a list with 10, 000 zeros and measure their execution times − import time # Initialize lists to store execution times for_loop_times = [] while_loop_times = [] list_comp_times = [] star_operator_times = [] # Run 500 iterations for reliable averages for ...
Read MorePython - Ways to merge strings into list
While developing an application, there are many scenarios when we need to operate on strings and convert them into mutable data structures like lists. Python provides several ways to merge string representations into a single list. Using ast.literal_eval() The ast.literal_eval() method safely evaluates string literals containing Python expressions. This is the recommended approach for converting string representations to lists ? import ast # Initialization of strings str1 = "'Python', 'for', 'fun'" str2 = "'vishesh', 'ved'" str3 = "'Programmer'" # Initialization of list result_list = [] # Extending into single list for x in ...
Read MorePython - Ways to iterate tuple list of lists
Lists are fundamental containers in Python programming used in day-to-day development. When dealing with nested structures like tuple lists of lists, knowing different iteration methods becomes essential for efficient data manipulation. Using zip_longest() with List Comprehension The zip_longest() function from itertools allows iteration across multiple lists of different lengths, filling missing values with None ? # using itertools.zip_longest from itertools import zip_longest # initialising list of lists test_list = [ [('11'), ('12'), ('13')], [('21'), ('22'), ('23')], [('31'), ('32'), ('33')] ] # printing ...
Read MorePython - Ways to invert mapping of dictionary
Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. Inverting a dictionary means swapping keys and values, so the original values become the new keys and vice versa. Using Dictionary Comprehension The most Pythonic way to invert a dictionary is using dictionary comprehension ? # initialising dictionary ini_dict = {101: "vishesh", 201: "laptop"} # print initial dictionary print("initial dictionary:", ini_dict) # inverse mapping using dict comprehension inv_dict = {v: k for k, v in ini_dict.items()} # print final ...
Read MorePython - Ways to format elements of given list
When working with lists containing numeric values, you often need to format them for better readability or presentation. Python provides several methods to format list elements, including list comprehension with string formatting, map() function, and modern f-string formatting. Using List Comprehension with % Formatting The traditional approach uses list comprehension with percentage formatting ? numbers = [100.7689454, 17.232999, 60.98867, 300.83748789] # Format to 2 decimal places using % formatting formatted = ["%.2f" % elem for elem in numbers] print(formatted) ['100.77', '17.23', '60.99', '300.84'] Using map() Function The map() function ...
Read MorePython - Ways to flatten a 2D list
Flattening a 2D list means converting a nested list structure into a single-dimensional list. Python provides several methods to accomplish this task, each with different performance characteristics and use cases. Using itertools.chain.from_iterable() The chain.from_iterable() function efficiently flattens a 2D list by chaining all sublists together − from itertools import chain nested_list = [[1, 2, 3], [3, 6, 7], [7, 5, 4]] print("Initial list:", nested_list) ...
Read More