Python lists explained in details for complete beginners

Having coded with Python computer programming language for quiet a long time now, I am completely aware of the importance of list object, especially when it comes to writing software that processes information. Through this article, I am going to explain the Python list object in details to the complete beginner.

A list in Python computer programming language is an object which is mainly being used to store other kind of objects such as strings, integers, floating points, tuples, dictionaries and for sure other lists. The syntax for declaring a list object in Python is very simple, completely easy to read and understand.

One can easily write a Python list by following the syntax shown below.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

l = []

As you can see from the above example, the syntax for a list object in Python computer programming language looks like a package in which other Python objects are being packed.

The following is another Python list object with some elements.

l = [1, ‘a’, (), {}, []]

Like many other objects in Python computer programming language, a list object has a length which one can easily find out by using the builtin len function like shown below.

len(l)

Once the above code is being executed successfully on the Python interactive shell, one will get the following output displayed on their console.

5

The Python coder can easily access the elements of a list object by using the operating of indexing like shown in the example below.

first_el = l[0]

The indexing starts from 0. So the first element in the above list has an index of 0, the second element has an index of 1 and so on.

To get our hands dirty and practice the concepts of lists in the Python interactive shell, try to access the second element of the list we have created by using the syntax which is shown below.

second_el = l[1]

One can easily check the value of a list element by using a conditional statement like it is being shown in the following example.

l[0] == 1

The value shown below gets displayed on the console once the above piece of Python code is executed in the Python interactive shell.

True

Being mutable objects, Python lists can be easily updated. The coder can change an element of a list by using the following syntax.

l[0] = 2

If the above piece of Python code is being executed successfully, the list will not remain the same. The above operation, gets the first element of the list named l, and replaces it with another element, in our case the element is the number 2.

To check if the list is updated or not, just type the following piece of Python code and see the result being displayed on the interactive shell for yourself.

l

Once the above piece of Python code is being executed on my Python console, the following is being displayed in the interactive shell.

[2, ‘a’, (), {}, []]

As you can see from the above output which is displayed on the Python interactive shell, the list has been updated, since the first element with an index of 0, does not equal anymore to the value of 1 like it did in the beginning when we created the list.

A simple check can prove it.

l[0] == 1

False

l[0] == 2

True

Python list specific operations

Like most of the Python objects, lists support specific operations which do work only for them. One can easily add a new a element to their Python list by using the specific method named append like shown in the following example.

l.append(‘new’)

A simple check to see if the new element has been added in the list or not can be done like shown in the example below.

l

The following comes up on the Python interactive console after the above piece of code is being executed.

[2, ‘a’, (), {}, [], ‘new’]

From the result being shown above, one can easily understand that the specific list method named append serves to update the list with new elements.

How about removing an element from the list, is there any specific method for doing it? Yes, the pop method can be used to remove a specific element from a list in Python computer programming language.

The Python syntax for the pop method, specific to list objects, is being shown below.

l.pop(0)

The above Python command, is being used to remove the element with an index of 0 from the list which is assigned to the variable l.

According to the official documentation, the specific list method named pop, takes as an input an argument, the index of the element which the user wants to remove from their list.

To check if the element with an index of 0, the first one in the list, is being removed or not, type the following command in your own Python interactive shell.

l

Once the above command is being executed in my Python interactive shell, the following list is being displayed on the console.

[‘a’, (), {}, [], ‘new’]

As you can see from the above result, the specific list method pop, is the perfect utility when it comes to removing an element from a list.

Let’s create a new list this time with the main purpose of practicing the specific methods which belongs to list objects, already builtin in Python.

t = [‘a’, ‘c’, ‘b’]

One can easily print the above list in the Python interactive console by using the already builtin Python print statement like shown below.

print(t)

The above command produces the following output on the Python interactive shell.

[‘a’, ‘c’, ‘b’]

A method which can help the Python coder to present the above list in the interactive console as part of the english alphabet, ordered according to the english alphabet, is the method called sort. It’s practical syntax and usage is being shown below.

t.sort()

To check the ordered list, just print is by using the Python builtin print statement like shown below.

print(t)

Once the above print statement is being executed in the Python interactive shell, the following gets displayed on the console.

[‘a’, ‘b’, ‘c’]

As you can see from the above output, displayed on the interactive Python console, the specific list method called sort, can be easily used to sort out the elements of a list object.

Create a new list with numbers as elements like shown below.

n = [0, 3, 2, 1, 4, 5, 6, 7]

Then try to sort the above list with the specific list method we explained.

n.sort()

After the list is sorted, try to print it like show below.

print(n)

Once the above Python statement is being executed, the following gets displayed on the console.

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

As you can see from the result which is shown above, the sort method sorted the numbers in the ascending order. How about sorting the elements of a list in the descending order, is there any builtin utility in Python computer programming language for achieving that?

Yes, there is. The syntax for it is being shown below.

n.sort(reverse=True)

The above argument, reverse=True, tells Python to sort the list in the opposite order; in this case in the descending order.

print(n)
[7, 6, 5, 4, 3, 2, 1, 0]

Final Thoughts

List objects in Python computer programming language serve a great purpose when it comes to building data structures. Rich in features, they support specific operations which can help the coder to perform actions such as sort the elements of the list, insert new elements in the list, pop out elements from the list, copy the list and many others which I am going to explain in details in another article.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *

*