Check For Word In String: Python 1 Liner

How can you check if a word is found in a string using Python? Recently, I had an issue where I wanted to exclude strings from a list if they contained a certain word. I thought I could use the following common code familiar to most using Python: i f # ' w d o o r d s ' o m i e n t h ' i h n o g w t o f i n d w o r d i n s t r i n g ' : But the problem ended up being a little more difficult than that. For example, what if you want to exclude the term word but not if that word is found inside other words, like sword? ...

July 14, 2021 · 6 min · 1217 words · Ryan Sheehy

Sort List Of Dictionaries By Key: Python 1 Liner

How can you sort a list of dictionaries by a key contained in each dictionary using Python? Can you do it in 1 line to show off your friends? Of course! To sort a list of dictionaries use the sorted() function with the key parameter set to a lambda function that uses the common key found in each dictionary that you want to sort the list by. Previously I posted a task where I needed to sort a list after the second element onwards, in this article I’ll use the same techniques but apply this to a specific key in each dictionary. ...

July 6, 2021 · 3 min · 515 words · Ryan Sheehy

Python: Sort List From Second (or Nth Index) On

How do you sort a list by the second (or nth) element on in Python? How can you leave the first or nth elements in a list as they are but then have the remaining elements in the list sorted? When applying the .sort() list method on a list Python will sort all elements in that list, but what if you only wanted to sort after a certain index number? ...

July 6, 2021 · 6 min · 1239 words · Ryan Sheehy

Reverse Order Of List In Python Without For Loops (Examples, One-Liners, No Imports)

There have been times when after creating a list in Python I’ve wanted to sort a list and then have the list reversed from ascending to descending. To reverse the order of a list in Python use either the .reverse() list method which mutates the original list, or the slice operator technique [::-1] which returns a new list. What are the quickest ways to get this task done? Here are two ways to do the same thing, but one method will mutate the original list when operated, whereas the other outputs a new list and keeps the original list as it originally was. ...

July 2, 2021 · 3 min · 504 words · Ryan Sheehy

How Many Spaces Is A Tab: 2, 3, 4 or 5?

In Microsoft Word you can control whether or not you want to see the white space characters in your document. This helps to check you haven’t incorrectly inserted a tab where a space should have gone and vice versa. Generally, a tab is the same width as 4 to 5 spaces provided the font being used equally sizes each character. For example, the Courier font’s tab equals 5 spaces, whereas the Arial font is 11 spaces to each tab when the font size for both is set to 12. ...

May 17, 2021 · 4 min · 679 words · Ryan Sheehy

How To Remove Characters From A String In Python (Examples, No Imports)

There are three main ways within Python on how to remove specific characters from a string in Python, and I’ve clustered these approaches based on the following methods: Built-in string methods By pattern By position Each approach has its own unique way of being able to perform the task required, so we will explore each with the use of examples to illustrate what might suit your use case best. Remove Characters Using Built-In String Methods The most popular methods of removing specific characters from a string in Python is through the use of 2 string methods: ...

March 16, 2021 · 9 min · 1713 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

List Changes Unexpectedly In Python: How Can You Stop It?

Why is it when you copy a list in Python doing b_list = a_list that, any changes made to a_list or to b_list modify the other list? If you’ve played with lists in Python you will reach a point where you want to copy a list to make modifications to it without changing the original list. Initially you would think the following would work: 1 2 3 4 5 6 7 >>> a_list = [1, 2, 3] >>> b_list = a_list >>> b_list.append(4) >>> print(b_list) [1, 2, 3, 4] >>> print(a_list) [1, 2, 3, 4] As you can see from the above example even though b_list was the only list which had an extra element added to it, a_list also changed! ...

9 min · 1739 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