How To Add An Empty Element To A List In Python

To insert or append an empty element into a Python list you need to first define what is meant by the word empty. By empty do you mean an empty string, None or something else? While I may use None in this post you can easily substitute it for something else according to your requirements. To add an empty element into a list either replace the existing element with the empty variable or if inserting it at the end use the list method .append() or if inserting it at a specific place use the other list method .insert(). ...

December 1, 2021 · 4 min · 768 words · Ryan Sheehy

How To Check If List Is Empty In Python

Similar to the previous post on how to check if a string is empty the same principles and methods apply when checking if a list is empty in Python. To check if a list is empty either use the direct approach by using an expression where a list is compared to an empty list or use the boolean expression with a not operator (i.e. if not my_empty_list:) or use the built-in function len() to see if the length of the list is 0. ...

November 26, 2021 · 10 min · 1919 words · Ryan Sheehy

Check Empty String Python - 3 Unique Approaches

How can you check if a string is empty in Python? To determine if a string is empty in Python use of the following three means available: see if the string has any length by using the built-in len() function, do a direct comparison using the expression == "" or convert the string to a boolean data type and if the result is false it is an empty string. Let’s look at each approach and see how they perform. ...

November 25, 2021 · 5 min · 1022 words · Ryan Sheehy

How To Change A String To Lower Case In Python

A way to easily sort a list of strings equally is to change all the strings to lower case, but how do you change strings to lower case in Python? To change a string to lower case in Python use the string method .lower() like this: my_string.lower(). Similarly, to change a string to upper case in Python use the string method .upper() like this: my_string.upper(). An example of how this works is as follows: ...

November 15, 2021 · 129 min · 27270 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

Python ord() Function (Code Examples)

What does the ord() function do? The built-in ord() function in Python converts any Unicode character into a number. It only takes one parameter which must be a single Unicode character, any more than one character produces an error. An example of the type of error you will get when sending more than 1 Unicode character is demonstrated here: 1 2 3 4 5 6 >>> ord('A') 65 >>> ord('ABC') Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: ord() expected a character, but string of length 3 found Notice how Python informs us of what is expected – a character, but a string was passed in of length 3. ...

November 11, 2021 · 6 min · 1158 words · Ryan Sheehy

Split A String Into List Of Characters In Python [Code Examples, One-Liner, No Imports]

A common requirement in Python is to split a string into the characters which make up the string. I’ve previously shown how you can do this by breaking up a word into separate cells using a spreadsheet, but how do you do this in Python? As a string is a data type that can be iterated it means each unit element within the string, being a character, can be referencing an index on the string. ...

November 11, 2021 · 3 min · 625 words · Ryan Sheehy

List Length In Python: Using len() Function

To find the length of a list use the built-in len() function which takes a single parameter that is iterable. For example this could be a string, list, tuple or dictionary and here is how it would look: 1 2 3 4 5 6 7 8 9 >>> my_list = ['a', 'b', 'c'] >>> len(my_list) 3 >>> my_dict = {'a': 100, 'b': 200, 'c': 300} >>> len(my_dict) 3 >>> my_str = 'How long is a piece of string?' >>> len(my_str) 30 This may be the easiest and quickest way of being able to find the length of a list and other data types, but what if you want to exclude certain items from your list? ...

November 11, 2021 · 3 min · 435 words · Ryan Sheehy

How To Use NOT Operator In IF Statement In Python [Code Examples]

One way to determine if one value does not equal another is to use the != comparator, but what if you’re not comparing two values – how can you determine if something is not true? Python has an inbuilt operator aptly called not which permits a user to check a variable, or a function’s result to test if the variable or returned value is not valid. Some classic use cases of this type of logic applied in my Python code have been checking if a file exists, other instances have been when scraping a web page and checking if the element existed on the page. ...

November 10, 2021 · 4 min · 850 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