Lists

Before we go over dictionaries, which are the most common datatype when sending and receiving information between applications (back-end to front-end, front-end to back-end, or back-end to websites, etc.), and this is especially true when interacting with the Algorand blockchain, I think it's important to have an understanding of how lists work.

Below is an example of a list:

my_list = [7, "Hello", False, 63.5]

A list can hold all types of data, and you can have all kinds of data in one list. You use lists when you need exactly that, a list. Lists are also mutable, as opposed to tuples—which means we can rearrange, extend, and replace items in a list, meaning they're super flexible!

A list is a collection of data enclosed between square brackets [ ], and separated by commas. An example of common kinds of data you would have in a list on Algorand could be one that has asset ID's.

asset_ids = [1265975021, 1138500612, 400593267]

Or perhaps a list of addresses:

addresses = [
'WWYUMYPM2Y5NIIZTF4O5N73A4ZTZQWXS6TNP23U37LQ6WWF543SRTGKWUU',
'7IWZ342UGNQ2JVS2E6EGFD4MPUNL4ZIWDYNFZIANR6U7WZXORCRQCCN3YY',
'HZ57J3K46JIJXILONBBZOHX6BKPXEM2VVXNRFSUED6DKFD5ZD24PMJ3MVA']

Although lists don't have to be organized in anyway and are not descriptive, they can be manipulated. For example, if you wanted to remove duplicate entries in a list, you could use the set() function.

my_list = [1, 1, 2, 3, 4, 4]
my_list_without_duplicates = set(my_list)
print(my_list_without_duplicates)
# Output:# [1, 2, 3, 4]

Let's look back at the first example of a list:

my_list = [7, "Hello", False, 63.5]

In the list above we have an integer at the first spot, 7; a string in the second spot, "Hello"; a boolean (True or False value) in the third spot; a float (AKA decimal value), in the fourth spot. I refer to the places these items are in the list as "spots", but the correct term is actually "indexes". We referenced them as the first, second, third, and fourth spot— however, in programming lists are zero-indexed. This means that we always start from zero, and use an integer to refer to their position in the list. This feels strange, but it is something you should have ingrained into your mind, as this is universal across all programming when indexing for positions in a list.

The correct reference to the positions would be Index 0 for 7, Index 1 for "Hello", Index 2 for False, and Index 3 for 63.5. But, how would we see this utilized in a programming scenario?

To interact with the list, we must first assign it to a variable:

my_list = [7, "Hello", False, 63.5]

Now, we can use index notation to pick out items of our choice by using their position, let's start with just printing the list to the terminal:

print(my_list)
# Output: [7, "Hello", False, 63.5]

... and now lets print the item at index 1 (the second item since the first item is always 0) using index notation:

first_index_my_list = my_list[1]
print(first_index_my_list)
# Output: "Hello"

Try printing the third index into the using index notation into the code editor down below, I've already defined the list for you below, click run when you're ready to run the code! The output should be 63.5

my_list = [7, "Hello", False, 63.5]"

Quiz

Question 1

What is the correct way to define a list in Python?





Question 2

Which of the following statements about lists is true?





Question 3

What will be the output of the following code?

my_list = [7, "Hello", False, 63.5]
print(my_list[2])




Question 4

What is the correct way to remove duplicate entries in a list?

my_list = [1, 1, 2, 3, 4, 4]
my_list_without_duplicates = set(my_list)
print(my_list_without_duplicates)




Code Editor