How To Create A List With Values: Python Code (Examples, One-Liners, No Imports)

You can construct a list in a certain number of ways as detailed in the Python documentation. The most popular I use the most is by defining my list variables using the [] list square bracket notation, for example as my_list = []. This works well when you want to start a list empty, but what if you want to be able to start with a list so that it is prepopulated with values? ...

October 13, 2021 · 4 min · 850 words · Ryan Sheehy

Remove Multiple Items From List By Index: Python (Examples, No Imports, One-Liners)

To remove an individual item from a list in Python you can use either a list method or the del statement, both approaches mutate the original list – meaning the list being operated on changes according to the instruction. To remove multiple items from a list you can use the del statement with the slice operator, or you can use a list comprehension with an if condition to filter the unnecessary items in your original list. Using the del statement will mutate the original list, whereas using a list comprehension will create a new list leaving the original untouched. ...

October 13, 2021 · 7 min · 1355 words · Ryan Sheehy

Python: Average Of List Of Numbers - Without Imports [One-Liner]

How do you calculate the simple average of a list of numbers in Python, without needing to import any libraries? Unfortunately, there’s no native average or avg function in Python to easily calculate the average of a list containing just numbers: 1 2 3 4 5 example_list = [1, 2, 3, 4, 5, 6] average(example_list) # NameError: name 'average' is not defined avg(example_list) # NameError: name 'avg' is not defined To calculate the average of a list of numbers in Python, use the sum function and the len function and divide the total sum by the total length of the list to obtain the average, like so: ...

July 20, 2021 · 3 min · 619 words · Ryan Sheehy

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

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