Creating and using Lists in Python

This post is to get a little more technical and get a deeper look into the usage of Lists in Python 3.

The usage of lists is very simlpe in Python3, they are much like arrays in other lenguages except for the little detail that that lists can contain any type of element inside one list. This means you can have numbers and words in the same list without a problem. 

In order to create a list we only need to type the elements of the lists inside brackets, and separated by comas:

A_list=["a",1,"greetings",5.6989]

Lists are zero based, that means that the first element of the array has the index 0, and the last one has n-1, where n is the total number of elements inside the list. 

A_list[0]

a

A_list[3]

5.6989

Also you can use negative indexes to start counting from the right side of the list, where the index is counted as [len(A_list)-n], so to acces the last element we use a [-1]

A_list[-1]

5.6989

Another useful thing about lists is that you can slice it, this means create from a part of the list a new list. This is done by using colons in the index brackets [ : ], where the first number is the index where you want to start the slice inclusive, and the other is the end exclusive. for example fi we wanted just the half of the list:

A_list[3:4]

Or when you don’t have an input it will automatically asume that are all the elements left, whenever it is after or before, of the list.

Adding elements

The next thing we can do with lists is adding elements, this can be done in several ways, an everyone has it’s unique way to add elements:

  • list.append( )

This one is used to add an element as the final element of the list. This method adds whatever you have inside the parenthesis as a new element, so you have to be carefull because if you can end up having a list inside a list.

>>>list.append( [3,4] )

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

As you can see the last element of the list is a list itself.

  • list.extend( )

This method is what is used to merge two lists into one, so if in the last example we used extend that would look different:

>>>list.extend( [3,4] )

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

  • list.insert( , )

The usage of the insert method is by having to arguments, the index where we want to insert the element, and the element that we want to insert. So the difference with the last two methods is that you can actually place the element in the place that you desire.

>>>list.insert(0,Y)

[Y,1,3,5,6]

Searching for elements

Another thing that you might do with lists, is to know how many times a value repeats itself inside a list. This is done with the count method. For example:

list=[3,3,4,"c","great","zen"]

>>>list.count(3)

2

But what about knowing if a certain value is on a list? Well you can just type :

>>> "c" in list

True

>>> "also" in list

False

Quite easy right? Well so far we can know if there is a value and how many times it appears in the list, but about knowing its position? Well that what the index method comes to the rescue. If we want to know the index of certain element we type:

>>>list.index(3)

0

>>>list.index("also")

Exception!

As you can see the index returns the first time a value appears in a list, and if we look for the index a value we better prepare to take care of an exception, so using the “in” operator before doing an index search woul be quite wise. 

Removing items

First of all we must know that lists in Python never have gaps, that’s right whenever we remover an element of the list the gap is filled by the next element so there are no blank spaces between them. There are two ways of removing and item:

  • del list[ ]

This way we eliminate the element of the list that is in that especified index, so if we wanted to erease the first element of a list we would type:

>>> del list[0]

[3,4,"c","great","zen"]

  • list.remove()

The other way to remove an element from a list is by looking for a given value, this is very useful when you don’t know where the element is located. For example:

>>>list.remove(3)

[4,"c","great","zen"]

There is another method to remove items from a list wich is very similar to the remove method, but this one has the extra that you can store the value you have removed. This is the pop method.

>>>list.pop("great")

great

>>>list

[3,3,4,"c","zen"]

As you can see the argument of the pop is shown.

Booleans of lists

The booleans of lists are very easy to understand, if you use a list in a boolean context you will get True whenever the list has something inside it; as long as the list isn’t empty, whetever it might contain you will get  True. And if the list is empty you get False, quite easy.

Here is a page I found very useful, it explains with many examples the different types in Python. 

This is my of my

 

CC BY 4.0 Creating and using Lists in Python by Manuel Lepe is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.