Posts

Showing posts with the label python-dictionary

Featured Post

Step-by-Step Guide to Reading Different Files in Python

Image
 In the world of data science, automation, and general programming, working with files is unavoidable. Whether you’re dealing with CSV reports, JSON APIs, Excel sheets, or text logs, Python provides rich and easy-to-use libraries for reading different file formats. In this guide, we’ll explore how to read different files in Python , with code examples and best practices. 1. Reading Text Files ( .txt ) Text files are the simplest form of files. Python’s built-in open() function handles them effortlessly. Example: # Open and read a text file with open ( "sample.txt" , "r" ) as file: content = file.read() print (content) Explanation: "r" mode means read . with open() automatically closes the file when done. Best Practice: Always use with to handle files to avoid memory leaks. 2. Reading CSV Files ( .csv ) CSV files are widely used for storing tabular data. Python has a built-in csv module and a powerful pandas library. Using cs...

Python Subset: How to Get Subset of Dictionary

Image
Here's a sample program to get the python subset. In this case, you'll find logic for dictionary subsets. Dictionary python To illustrate, I have taken a dictionary as below with keys and values. my_first_dict = { 'HP': 100 'IBM': 200 'NTT': 300 'ABC': 400 'GDF': 500 } I want to make a subset of values greater than 100 and less than 400. How can you achieve this? No worries, below, you will find the logic. Logic to get subset out of a dictionary I am using dictionary comprehension to achieve this. Syntax: sub_set = { key:value for key, value in my_first_dict.items() value >100 and value <400} Result References Python Programming: Using Problem-Solving Approach