Python collections type: List

Subscribe to my newsletter and never miss my upcoming articles

List is one of the collections data type in Python programming that are always ordered and allows the values in it to be changeable.

Collection sound a new word right?

In Python programming language, there are basically four collection data types. Collections data types are types used to store large number of variables, data set or information. They are list, tuples, set and dictionary. For this article I will only be focusing on the list collections data type. It is important to understand all collections type and their properties before choosing the one to use for a given data set or information.

images.png

So let's go.

List is a collections data type that is ordered and it's information can be changed, they are mostly identified by square brackets, this is because the most common way in which they are written are in square bracket. A list can have different data types ;strings,integers,boolens, decimals, but they are separated by comma. So a list can also be said to be a collection data type which comma separated elements of different data types.

images (2).png

Creating a list:

It is very easy to create a list.

You can create a list in two ways.

First: you can create a list by defining the elements in a square bracket assigning it to a variable. '

the_list= [ '1', 'cat', 'girl', 'school']

And you can simply print it using a print statement.

print(the_list)

The second method is by defining your variables without the square bracket. IMG_20200617_101304_725.jpg

Then it prints out a list.

IMG_20200617_101233_281.jpg

So easy right?

Some attributes of list are:

  1. Information or data set in a list is always ordered: This means that the order in which you defined or specified your elements in a list remains like that, it doesn't change when printed out, each element still has it's position.

a =[ 'boy', 'girl', 'mother']

b =['mother', boy, girl]

IMG_20200617_040121_649.jpg

when this is printed a is not equal to b, this is because the elements in each list don't have the same position even though they have the same elements.

IMG_20200617_040053_268.jpg

Clear right?

  1. A list can contain any number of elements and it accepts elements of different data type.

my_list = ['True', '1', 'big', '0.1']

print(my_list)

Accessing the elements in a list :

One interesting thing about list is that you can access the individual elements in a list, group of elements can also be accessed. There are different method in which this can be done some of them are indexing and slicing. So let's discuss this in details.

Indexing :

Elements in a list can be accessed by referring to their index number. Index number just show their position when numbered or counted and in Python counting of elements or values starts from 0 not 1 as seen in the image on indexing above.

Example:

fruits = ["banana", "mango", "pawpaw", "orange"]

In counting the above, banana has the index of 0, mango has 1, pawpaw has 2 and orange 3.

So to access the second item in the list which is mango, we simply do;

print(fruits [1])

Easy right? Don't forget that python starts counting from zero not one.

We also have another indexing method which is negative indexing, this method simply means beginning to count from the end with -1 signifying the last item, then -2 , -3 coming forward from the last item.

So to print the last item from our fruits list, we simply do;

print(fruits [-1])

In using indexing, the range of indexes can also be specified, this simply means telling your indexing where to start and where to end the range, that is indicating two index; the start and the end.

So adding more items to our fruits list. we now have;

fruits = ["apple", "banana", "cherry", "orange", "guava", "pawpaw", "cashew", "mango"]*

print(fruits[3:7])

Note that the print will start at index 3 (included) and end at index 7 (not included).

Also,remember that the first item has index 0.

Run the above on your shell and confirm the answer.

So easy right?

Let's continue

In using range of indexes we can also decide to leave out the first index. that is not specify it, just specifying our last index.

Let's see it;

fruits = ["apple", "banana", "cherry", "orange", "cashew" , "guava", "pawpaw", "mango"]

print(fruits[:4])

this returns the elements from apple to orange.

Range indexing can also be done by leaving out the end index, this simply means that the range will begin from the start index and go on to the end of the list.

Going back to our fruits list;

fruits = ["apple", "banana", "cherry", "orange", "cashew" , "guava", "pawpaw", "mango"]

print(fruits[3:])

The print statement above returns the elements starting from index 3 which is orange to the end of the list which is mango. The range of negative indexes can also be printed. This is usually done if the printing wants to be started from the end of the list just as it is done in negative indexing but this takes two indexes; the start and end indexes.

Example:

fruits= ["apple", "banana", "cherry", "orange", "cashew" , "guava", "pawpaw", "mango"]

print(fruits[-1:-5])

print(fruits[-5:1])

print(fruits[1:-5])

The print statement above returns the items from index 1 which is banana to index -5 excluded, but takes the next element coming forward from the end index. So I have

["banana", "cherry"]

Kindly confirm it using your shell. Remember the start index is included while the end index is excluded.

Indexing can also be used to change the value of an element in a list by just referring to the Indexing number.

Example: To change the value of banana in our list to pineapple.

fruits= ["apple", "banana", "cherry", "orange", "cashew" , "guava", "pawpaw", "mango"]

Banana is the second element with index of 1. We simply do;

fruits= ["apple", "banana", "cherry"]

fruits[1] = "pineapple"

print(fruits)

IMG_20200617_040510_568.jpg

The banana in the list have been replaced with pineapple.

Slicing:

images (1).png This method is used to print elements of specified range from a list. It can use it to get elements from a list using two ways. The first method is slicing using the start and end index just like the range of indexes method explained above. The second method is by specifying a an increment argument. This can also be done using the slice object function.. The general method for slicing is: slice(start, stop, increment).

Example: to slice our fruits list with start 3, stop 7 and increment or step 2.

fruits= ["apple", "banana", "cherry", "orange", "cashew" , "guava", "pawpaw", "mango"]

To solve this, we simply assign our sliced fruit to a new variable.

new_fruits=fruits[3:7:2]

print(new_fruits)

IMG_20200617_035935_491.jpg

If you run it on your shell, your answer will be orange and guave.

IMG_20200617_040002_641.jpg

Did I hear you ask how?

So here is it;

In slicing , the start index is included which is 3 and from our list index 3 is orange, then it starts counting for our increment from the start index, so it starts counting our increment of 2 from Orange but it doesn't count from zero but from one and our first increment is guava, the 7 in our slicing tells it to end when it gets to position of elements 7. Even if we have a large list, it stops slicing when it gets to element with position 7 on the list.

Here's another example with end index 9.

IMG_20200617_035906_503.jpg

IMG_20200617_035834_877.jpg

Clear now? Good

Finally in slicing, the start index tells it where to start from, the middle index that is the end index tells it the position to stop slicing and the step index or the increment index tells it how to increment and it is optional , if not specified the default is 1.

For more on list and slicing, click here

No Comments Yet