Iterable Variables

Try me

Open In ColabBinder

Introduction

If we say that iterable variable are objects that can be iterated over, you will probably ask, what does iterate over mean?. In a nutshell, iterating over a variable means that the variable contains a collection of items, and that we will be able to use the variable to easily repeat the same process on each item in the collection. The process of repeating a process over a collection of values is called iteration. For example, if we have a list of numbers, we can iterate over the list and print each number. We will learn more about iterating in the next section on control structures, but for now, we can think of iterable variables as collection of items. The main types of iterable objects are lists, tuples, and dictionaries, but guess what type of primitive variable type is also an iterable variable? You are right! strings are collections of characters! Let us provide an introduction to lists first, which are the most basic type of iterable, and later see how to use lists methods and properties with strings. Finally, we will cover tuples and dictionaries, and provide you some extra cool tips.

Lists

Lists are the most basic type of array variables. They are just a collection of items, not necessarily of the same type. To create a list, we use the [] notation. This will create an empty list, with no items or members in the list. We can use the built-in method append() to append a new member to the list, the built-in method pop() to remove a member from the list, and the built-in function len() to get the length of the list:

[ ]:
# Create empty list with square brackets
mylist = []
print("the length of the mylist is ", len(mylist))
mylist.append(34)
print("the length of the mylist is ", len(mylist))

mylist.append("hello")
print("the length of the mylist is ", len(mylist))

mylist.append(5.0)
print("the length of the mylist is ", len(mylist))

mylist.pop()
print("the length of the mylist is ", len(mylist))

Lists can also be initialised with a collection of values, just providing a comma separated list of values between the brackets.

[ ]:
mysecondlist = [1, "Hello", 2]
print("the length of mysecondlist is", len(mysecondlist))

Note that they can contain any type of variable, and they can contain as many variables as you wish. Regardless of the initialization, the list is always mutable and can be modified:

[ ]:
mysecondlist.append("Oranges")

Indexing

We can select a member of a list by appending to its name its index between brackets. The index is an integer number used as a key to reference each member of the lists. Indices go from 0, to access the left most element and increase 1 by 1 to access consecutive members from left to right until reaching the length of the list -1 (the right most element). For example, if we want to access the first member of mylist, we can write mylist[0], and mylist[1] to get the second member:

[ ]:
print(mylist[0]) # prints first element
print(mylist[1]) # prints second element
print(mylist[2]) # prints third element

Ooopsie, we have gone too far! since we popped one element of our list, the length of the list is now 2, and therefore Python raised an ‘IndexError: index out of range error’ when we tried to access the third element. Beware of this indexing errors when dealing with lists.

Indices also work in reverse order, from right to left, using negative values. So if we want to access the last element of the list, we can write mylist[-1], instead of mylist[len(mylist)-1]. Thus, we can traverse the list from right to left using decreasing negative values, mylist[-2] to get the second last element, and so forth:

[ ]:
print(mylist[-1]) # prints last element
print(mylist[-2]) # prints second last element

One more tip, you can pass the index of the element you want to remove from the list as an argument to the pop() method. For example, if we want to remove the first element of the list, we can write mylist.pop(0):

[ ]:
mylist.append("again")
mylist.pop(0)
print(mylist)

Also, the members of a list can be modified:

[ ]:

fruit_list = ["Red", "Green", "Blue"] print(fruit_list) fruit_list[1] = "Orange" print(fruit_list)

One more tip, you can pass the index of the element you want to remove from the list as an argument to the pop() method. For example, if we want to remove the first element of the list, we can write mylist.pop(0):

[8]:
mylist.append("again")
mylist.pop(0)
print(mylist)
['hello', 'again']

Also, the members of a list can be modified:

[3]:

fruit_list = ["Red", "Green", "Blue"] print(fruit_list) fruit_list[1] = "Orange" print(fruit_list)
['Red', 'Green', 'Blue']
['Red', 'Orange', 'Blue']

Arithmetic operators with lists

Python allows to use some basic arithmetic operators with Lists. For instance, we can use the addition operator + to join/concatenate lists together:

[9]:
first = [1, 2, 3]
second = [4, 5, 6]
print(first + second)
[1, 2, 3, 4, 5, 6]

We can use the multiplication * operator with an integer operand to replicate lists as many times as the operand. For instance, if we want to replicate the list first three times, we can write first * 3:

[10]:

print(first * 3)
[1, 2, 3, 1, 2, 3, 1, 2, 3]

Indexing and slicing

We already showed that it is possible ot access individual members of a list with indices, by specifying the index of the member between brackets. Note that if the index is greater or equal than zero, it tells us the position of the member from the left of the array, and if the index is negative, it indicates the position from the end of the array.

Slicing is used to fetch a given number of subsequent (e.g. consecutive) members of a list.

The basic slicing syntax is [a:b] where b > a, it will return the members from a to b-1 from left to right:

[12]:
my_list = [1, 2, 3, 4, 5, 6]
print(my_list[0]) # non-negative index: from the beginning of list
print(my_list[0:2]) #Slicing the two first members
print(my_list[-3:-1]) #slicing from the end of the list
1
[1, 2]
[4, 5]

We can add a third parameter c to control the slice sequence [a:b:c] will return the members from a to b-1 in c steps:

[19]:
print(my_list[0:4:2]) #Slicing the first two members and jumping by 2
print(my_list[0:-1:2]) # remember that the index of the last member is -1
[1, 3]
[1, 3, 5]

If c is negative, we will traverse the list in reverse order, from the end to the beginning:

[ ]:
print(my_list[3:0:-1]) # slicing in reverse order
print(my_list[::2]) # Slicing odd members (leave empty to use first and last)

By default, the first parameter of a slice is 0, and the default parameter of second is -1, and the third is 1, so we can leave any parameter blank to use these default values:

[20]:
print(my_list[::-1]) # slicing all members in reverse order
print(my_list[::-2]) # slicing all members in reverse order and jumping by 2
print(my_list[:2]) # slicing all members from the first two members

[6, 5, 4, 3, 2, 1]
[6, 4, 2]
[1, 2]

Logical Operator in

The logical operator in returns True if an array contains a specific member:

[21]:
name = "Paco"
if name in ["Paco", "Pepe"]:
    print("Your name is either Paco or Pepe.")
Your name is either Paco or Pepe.

Strings

As mentioned above, string variable are also iterable variables, meaning that we can use indexing:

[6]:
text = "Hello world!"
print(text[1])
print(text[5])
e

And slicing!

[4]:
text = "Hello world!"
print(text[3:9])
lo wor

As with lists, if you want to indicate the firts index or the last index, you should use empty indexes:

[10]:
text = "Hello world!"
# Prints from index 5 to the end
print(text[5:])
# Prints from the beginning to index 7 (excluded)
print(text[:7])
# Prints from the beginning to the end, which is the same as print(text)
print(text[:])
 world!
Hello w
Hello world!

Can you use indexing to print a text in reverse order? Yes you can!

[6]:
text = "Hello world!"
print(text[::-1])
!dlrow olleH

So, by all means strings are iterable objects and therefore we can select individual characters as members, but recall that, just as any other built-in type, strings are immutable objects and therefore their members cannot be modified:

[1]:
text = "Hello world!"
print(text[0])
text[0] = "h" #this will not work
H
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-6c65d70045ae> in <module>
      1 text = "Hello world!"
      2 print(text[0])
----> 3 text[0] = "h" #this will not work

TypeError: 'str' object does not support item assignment