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

Python List Not Appending? Troubleshooting - Solutions for Beginners

Why does a Python list not append an item? When you use the list method .append() to insert a list item to the end of its existing contents, you could be in for a surprise if this doesn’t happen in some instances. The main reasons for why this may occur are due to the improper usage of the list method or adding a variable that doesn’t actually contain the information expected. ...

October 10, 2023 · 9 min · 1807 words · Ryan Sheehy

2 Ways To Create A Python List With Identical Elements: One-Liners

How can you create a list with identical elements in it with Python? There are two ways to create a list with identical elements in Python. One method is fairly straightforward and easy to remember, whereas the other involves the itertools library. The one I apply the most in my Python coding is the method that uses the asterisk operator to a list containing the item I want to replicate. The other method uses the resources and techniques of a library with a function that is difficult to forget: itertools.repeat() ...

June 11, 2023 · 5 min · 913 words · Ryan Sheehy

Fix NameError: Python List Is Undefined

How do you fix the NameError: Python list is undefined type errors? When you get this error in your Python code there are a few ways to fix the problem and in essence, the easy fix is to check you’re working on a list variable that has been initiated as a list. The three common causes I have found in my own Python code for why this error pops up is due to: 1. not instantiating a variable as a list; 2. incorrectly typing the name of the list variable; or 3. using a variable in a list that hasn’t been declared. ...

June 11, 2023 · 5 min · 929 words · Ryan Sheehy

Python List Not Sorting: Troubleshooting With Examples

Why does a list not sort properly in Python as expected? There are a couple of reasons why a list may not have sorted as expected in Python. Here are a couple of ways to diagnose the problem to help you check. Here’s a quick list (no pun intended!) you can use to quickly check: Are all elements within the list of the same data type? Look at setting them to the same data type using the key parameter Are you capturing the result of a .sort() method? Change this sort operation to the sorted() function Are you sorting items with mixed case? Apply the key parameter converting all items to .lower() Do you understand the key parameter? Extract the lambda function to check it works on elements in your list. Are you sorting in reverse? Check the reverse parameter to your sort function. False is the default setting it this parameter is not defined. Are you sorting unicode characters? Look to use a standard encoding type with the key parameter using lambda x : x.encode('utf-8'). Are there any whitespace characters wrapping the elements? Apply the .strip() string method to each element with the key parameter. Let’s explore each of these items in a little more detail with examples. Here are some reasons why your Python lists may not be sorting properly: ...

June 10, 2023 · 7 min · 1289 words · Ryan Sheehy

Python Lists: Order Alphabetically & Sorting Examples

How do you sort a Python list alphabetically? There are two easy ways to sort a list in Python: using the standard sorted() function or using the .sort() list method. Assuming you have a list of strings you can apply either method to sort the list alphabetically. If you have a list of numbers and strings you can use the parameter key to change all values within the list to a str data type and then perform the necessary sort (more on that later). ...

June 10, 2023 · 9 min · 1854 words · Ryan Sheehy

List Comprehension In Reverse Order With Python

How do you perform a list comprehension in reverse using Python? A list comprehension is generally a short one line code that allows you to create a new list by iterating through an iterator (such as another list). The syntax for a list comprehension is [expression for variable in iterable if condition] with the if condition part being optional. The easiest way to perform a reverse iteration is to use the reversed() function on the iterable so that the list comprehension syntax is written as: [expression for variable in reversed(iterable)] . ...

June 9, 2023 · 5 min · 940 words · Ryan Sheehy

How To Remove Leading Zeroes With Python Dates

How do you remove leading zeroes in Python formatted dates? I have been sending data through to a Suitescript API endpoint and have received the error that the date information I am sending through needs to be of the format D/M/YYYY, here’s the error specifically: " " " t n m y a e p m s e e s " " a : : g " " e e I " r N : r V " o A I r L n . I v S D a u _ l i F i t L d e D S _ d c V a r A t i L e p U t E v E " a r , l r u o e r " ( , m u s t b e D / M / Y Y Y Y ) " . . . Currently, the way the date is formatted and sent from my Python code is as follows: ...

June 3, 2023 · 3 min · 478 words · Ryan Sheehy

Convert JSON To CSV In Python: Complete Code With Example

How do you convert JSON to CSV using Python quickly and easily? If you’re looking to store data received from an API that responds with JSON data and want to convert this to a CSV file then Python can easily help. Simply import the standard json and csv libraries into your Python code then read the json data into a variable and construct it to a one-dimensional format that can make it easy to store the data into a csv file. ...

June 3, 2023 · 6 min · 1222 words · Ryan Sheehy

JSON.dumps Not Able To Serialize Object

If you have tried to serialize an object in Python into JSON you may have come across an oblique error that mentions you’re unable to serialize it, but what does this mean? I recently had an example whereby I needed to transfer a dict object through to an external API endpoint and I thought by simply using the json.dumps() function that I would be able to create a json object to send through to the API. ...

June 1, 2023 · 1 min · 189 words · Ryan Sheehy