Read A CSV File To List Of Dictionaries In Python

The most common form of data I have played with is generally found in CSV format. This makes the data easy to understand but difficult to modify or change, especially when the CSV file contains lots of columns. In this article I will demonstrate how I frequently use this file format to import it into a useable data structure in Python, namely, a list of Python dictionaries. Let’s start with defining the basic terminology so we’re on the same page before jumping into some examples. ...

January 20, 2024 · 21 min · 4348 words · Ryan Sheehy

What Does Asterisk Before Variable Mean In Python?

What does the asterisk before a variable name mean in Python? For example, if I have a variable that contains a list of strings and the name of the variable is called my_list what does *my_list do? The asterisk is an operator in Python that is commonly known as the multiplication symbol when used between two numbers (2 * 3 will produce 6) but when it is inserted at the beginning of a variable, such as an iterable, like a list or dictionary, it expands the contents of that variable. ...

March 12, 2022 · 6 min · 1150 words · Ryan Sheehy

Python Initialize Dictionary: Use Dict Constructor Or Dict Literal?

How do you initialize an empty dictionary in Python? Is it best to use dict() or {}? There are two common ways of initializing a dictionary in Python. One involves using the built-in function dict() and is known as the dict constructor, whereas the other method is known as dict literal and has the syntax {}. Here’s an example demonstrating how to initialize using both approaches in the Python REPL: ...

March 10, 2022 · 5 min · 957 words · Ryan Sheehy

Sort Dict By Value Or Sort Dict By Key In Python: 1 Liners

It can be easy to sort a list, whether a list of strings or even a list of dictionaries, but can you sort a dictionary? One way to sort a dictionary by the values of each key : value pair in Python is to use a dictionary comprehension. Dictionary comprehensions are very similar to list comprehensions and have the following schema: 1 new_dict = { key: value for (key, value) in original_dict.items()} The first expression within the dictionary comprehension, the key: value portion, is the basic structure for setting any key and value pair within a dictionary. The second expression is familiar as the one-liner for loop, and within this statement we have a tuple representing the key and value returned from the dictionary’s .items() method. ...

November 12, 2021 · 6 min · 1136 words · Ryan Sheehy

Enumerate Dictionary In Python: Video Demonstration

Previously I looked at how to use the enumerate() built-in function with a for loop to provide two variables when iterating through a list. In that article the enumerate() function provides both an index number and the element in each list by wrapping these together into a tuple. But what happens when the enumerate() function is applied to a dictionary? As done previously let’s inspect the output of the enumerated dictionary by printing its contents: ...

November 9, 2021 · 2 min · 403 words · Ryan Sheehy

Create A Dictionary In Python: Quick 5 Minute Beginners Guide

How do you create a dictionary in Python? Dictionaries in Python are mutable data types that contain key: value pairs. A dictionary can be a great way to organise information, especially if checks need to be performed prior for uniqueness and/or the value of that key needs to be changed, skipped or inserted. Creating A Dictionary Dictionaries can be created using either the dict() constructor method, using the dict literal annotation {} or using a dict comprehension. ...

9 min · 1865 words · Ryan Sheehy

Sort Nested Dictionary By Key Or By Value In Python: One Liner

How do you sort a nested dictionary in Python by key or by value? Throughout this article, I will use the following example, which displays a nested dictionary that contains three main properties labelled dict1, dict2, dict3 and from each key they reference another dictionary which contains a simple dictionary of three keys labelled A, B and C with each key matched to the same value 1, 2 and 3: ...

5 min · 953 words · Ryan Sheehy