Python: 5 Best Ways to Create List of Tuples from Dictionary

πŸ’‘ Problem Formulation: In Python, dictionaries are a collection of key-value pairs, and sometimes there’s a need to convert this structure into a list of tuples for various reasons such as data manipulation or output formatting. Let’s suppose we have a dictionary like {‘Alice’: 24, ‘Bob’: 27, ‘Charlie’: 22} and the goal is to transform … Read more

5 Best Ways to Create a List of Tuples From Two Lists

βœ… Problem Formulation: How to create a list of tuples by combining elements from two separate lists. Typically, each tuple is formed by taking one element from each list and pairing them together based on their position within their respective lists. For example, given List A: [1, 2, 3] and List B: [‘a’, ‘b’, ‘c’], … Read more

How to Return Multiple Values from a Function in Python

Understanding Functions in Python πŸ’‘ A Python function is a block of reusable code that performs a specific task. Functions help break your code into smaller, more modular and manageable pieces, which improves readability and maintainability. Python functions are defined using the def keyword, followed by the function’s name and a pair of parentheses containing … Read more

Python Tuple Concatenation: A Simple Illustrated Guide

Python tuples are similar to lists, but with a key difference: they are immutable, meaning their elements cannot be changed after creation. Tuple concatenation means joining multiple tuples into a single tuple. This process maintains the immutability of the tuples, providing a secure and efficient way to combine data. There are several methods for concatenating … Read more

Sort a List, String, Tuple in Python (sort, sorted)

Basics of Sorting in Python In Python, sorting data structures like lists, strings, and tuples can be achieved using built-in functions like sort() and sorted(). These functions enable you to arrange the data in ascending or descending order. This section will provide an overview of how to use these functions. The sorted() function is primarily … Read more

Python Return NamedTuple From Function

In Python, you can use the collections.namedtuple() function to create a tuple with named fields. Here is an example of a function that returns a named tuple: In this example, the get_person_info() function creates a Person named tuple with name, age, and height as named fields. The function then creates an instance of this named … Read more

Python Find in Sorted List

Python’s built-in sorted() function enables programmers to sort a list efficiently and easily. On the other hand, the list.sort() method provides an in-place sorting mechanism. Additionally, Python allows for custom sorting using the key parameter in these functions, enabling more advanced sorting scenarios. In this article, I’ll show you how to find an element in … Read more