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

Sort List By String Length: Python Code Examples (No Imports)

To sort a list of strings according to their length, use a lambda function on the key parameter with the lambda function using the len function on each element. This is represented as follows using the sorted function: sorted(my_list, key=lambda el: len(el)). Here is a simple example demonstrating the use of this code: 1 2 3 4 my_list = ['Smithers', 'Smiths', 'Sims', 'Smalls'] my_sorted_list = sorted(my_list, key=lambda el: len(el)) print(my_sorted_list) # ['Sims', 'Smiths', 'Smalls', 'Smithers'] Or, if using the .sort() list method would be represented as follows: ...

October 3, 2021 · 3 min · 522 words · Ryan Sheehy

Python: Empty String To INT

To convert a string to an integer you use the handy int() function. For example, if you have a string such as "79" and you insert this like so into the int() function: int("79") you get the result 79. But what happens when you don’t have a string that easily converts into a number? Here are some examples where converting a string into an integer type simply will not work in Python (for obvious reasons): ...

September 24, 2021 · 5 min · 940 words · Ryan Sheehy

How To Flatten A List Of Lists In Python (Examples & No Imports)

Recently, I had an exercise where I needed to flatten a two-dimensional list down to just one dimension, something where I needed the result to be like this: [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] = > [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] There were a couple of ways I completed this task, one involved using the common for-loop process but as a one-liner, another involved using a standard Python function, and the third way introduced me to the concept of recursion in Python. ...

August 23, 2021 · 15 min · 3040 words · Ryan Sheehy

Is A List A Data Type?

In Python there is a handy function to determine the data type of any variable, and it is aptly called type(). This function can help to assess whether a variable is of a certain data type to help you perform any type of computation on it. The different data types available in Python are: int, float, str, dict, list, and tuple. Therefore, list is a data type and can be checked using the type() function. ...

August 23, 2021 · 3 min · 508 words · Ryan Sheehy

Python For Loop One Liner With IF Conditions [Code Examples]

There have been times when I wanted to perform a simple for-loop filter operation on a list, and I’ve often wondered if there’s a quick and simple way to do this without having to import any libraries. What I discovered is that there was an easy way, and what’s awesome about it is that it can be done in one simple line! If you’ve been operating with dictionaries or lists, you would have likely come across a need to loop through each key or element within those structures to only obtain a certain set of data from it, or to obtain a new modified set of data from the original structure. ...

July 30, 2021 · 6 min · 1236 words · Ryan Sheehy

Python: Get Average Of Two Dimensional List - No Imports [Code Examples]

In my previous article where I found the average of a list in Python without using imports, in this article I want to expand how you could apply the same concept, but to two-dimensional lists. Thankfully, if you know how to find the average of a one dimensional list in Python, you can simply expand this knowledge to finding the average of a two-dimensional list. The big question to ask, when finding the average of a two-dimensional list, is seeking what is it that you want to average: is the whole list, or just an average of each row, or is it an average of each list? ...

July 29, 2021 · 6 min · 1243 words · Ryan Sheehy

How To Sort 2D Array In Python: 1 Line Of Code Without Numpy

How do you sort a two-dimensional array in Python easily without importing libraries? Thankfully, some native functions in Python make sorting arrays a breeze. I recently had a project where I had the following data structures, representing each unpaid invoice by the customer, the days they were overdue and how much was outstanding. Here’s a simplified sample of the data I was working with (the first row contains the name of the columns of the array): ...

July 29, 2021 · 7 min · 1399 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