Python List And For Loops In Power BI - Enterprise DNA

Python List And For Loop In Power BI

No comments

In this tutorial, I’ll demonstrate how For Loop can iterate over Python list elements in Power BI. Take note that a list is a Python data type that consists of elements ranging from the simple to advanced.

The For loop enables us to repeat codes that are similar for a limited number of times. It simplifies complex problems into simple ones.

To start with, you need to create a new Python Notebook using your Jupyter Notebook. Once you have your Python Notebook open, we can now make our Python List in the code cell. 

Making A Python List And For Loop in Power BI – Numerical

Let’s call our sample list my_list. A Python list is indicated by brackets ‘[ ]’ and separated through comma ‘,’.

python list

Make sure to put your elements inside the brackets or else, nothing will appear when you run it.

python list

Let’s say we want to add 1 to all of our elements within our list. You can’t simply input +1 in your code because it will result to a typing error. To do so, we have to iterate each element in our list by using For Loop

python list

We use the word For to indicate a loop followed by a placeholder also known as variable. Usually, we use variables such as i and x but for now, let’s use i. Again, we want to add 1 to each of our elements. So, we need to iterate over each item in our list by typing the code below.

python list

Then wrap it in a Print statement. Once we run that, we can see that we’ve successfully added 1 to each of our elements in our Python list.

python list

Just know that you can always change and add any number you want. For example, we want to add 5. Simply type in the number inside the parenthesis. Once we run it, it will show in our findings that each of our elements are added into 5.

python list

Appending Existing List To A New List Using For Loops

We might want to indicate our findings from our previous list to our list. Let’s use 6, 8, 9, 10, and 11 findings as examples. Let’s first create an empty list in a new code cell where we can incorporate our previous list ‘my_list’ into it.

Let’s call this empty list ‘new_list’ and make a For Loop using X as our variable. We will append our existing list called my_list, so we’re going to iterate on that.

python list

Then, we want the values of our my_list to be added to 5 so we need to add x = x+5. We’ll now append our previous list to our new_list with the variable X. Append is used to add an element to the end of an already existing list.

python list

In a new cell, we can type in our variable new_list. Once we run this, we can see that we have successfully added our findings to our new list.

python list

Making A Python List And For Loop In Power BI – Text

I’ve demonstrated a few ways on how we can create Python list and For Loops through numbers previously. Now, let’s evaluate some text in the same way. I’ll be using company names in this example.

Let’s type in the word company, add our brackets, and list our company names inside. For this example, let’s use Microsoft, IBM, Tesla, and Meta. Note that you can use any company you like. 

python list

With the use of For Loop, we want to return the company with the letter “a” in their titles. To classify those companies, create a For Loop by typing in the code below.

python list

This code indicates that by using For Loop to go through each element in our list, we can print each one of these elements that have the letter “a” and then run it.

We can already see how convenient and powerful this method becomes when we look at looping through thousands of different elements that may be in a list. Moving on with our tutorial, I’ll show you another method called List Comprehension.

Making A Python List And For Loop In Power BI – List Comprehension Numerical

List Comprehension is quite similar to what I have previously demonstrated. The difference lies with it being much neater, easier, and time-efficient. It is preferred by some people because it can limit the number of lines used in a program. Let’s create the same action on our first and second example of adding 1 directly in each element within the same code cell. 

To avoid confusion and to provide a comparison, let’s name our list my_list2 and then type in our Python list elements inside the brackets. Under that, we’ll create a new list ‘new_list_2,’ to indicate our expression of adding 1 to each of the elements listed in my_list2.

Once we run our new_list_2 in a new cell, we’ll see the same findings or results of elements from before.

We can see that both methods provide the same outcome; however, using list comprehension is a lot shorter, quicker, and easier to do. 

Making A Python List And For Loop In Power BI – List Comprehension Text

Lastly, another way we can use list comprehension is by using it in evaluating text same as our previous example regarding company names. Let’s do the same action we did last time, where we iterated each companies to get our particular element.

Let’s use a different name to provide a comparison, for example, a_company. We want to return an element (i) for each element (i) in our company list if it has ‘a’ in that particular element (i). See the code below.

Once we type in our a_company list on a new cell, it will show us those elements or companies that have a letter “a.”

We can also use For Loop in fixing elements within a list. Using the same example, let’s say we want to make all the elements in our Python list in uppercase.

We’ll copy that same company list written in lowercase before and type in our For Loop under that. We need to use the upper() method to have each element within our list in uppercase.

Once we run that code, it will show that all the elements within our Python list are now typed in uppercase.

***** Related Links *****
Python User Defined Functions | An Overview
How To Load Sample Datasets In Python
Using Python In Power BI | Dataset And String Function

Conclusion

The methods I’ve demonstrated are just a few of all the possible things you can do using Python list and For Loop in Power BI.

A list is the most versatile datatype available in Python, written as a list of comma-separated values or items between brackets. A Python list helps store multiple data at the same time.

On the other hand, For Loop is crucial in Python as it enables users to traverse lists that may contain thousands of elements.

All the best,

Gaellim

Enterprise DNA Power BI On-Demand

***** Related Course Modules *****
Python I for Power BI Users
Python II for Power BI Users
R for Power BI Users II

***** Related Support Forum Posts *****
Calculate Date Difference Between Two Consecutive Rows Grouped By Column Using Python Script
Python for Power BI Users – Three Ways to Use Python Scripting
Error In PBI Using Python Script

For more Python script queries to review, see here…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.