Advertisement
  1. Code
  2. Python
Code

A Smooth Refresher on Python's Lists

by
Languages:
This post is part of a series called A Smooth Refresher To Python.
A Smooth Refresher on Python's Dictionaries

Python is a wonderful language. Well, it at least taught Gloria to love again! This is an expected thing to happen with a language packed with many attractive features. It is free and open source, simple, easy to learn, etc. It is thus no surprise that Python came in 5th place among 20 other programming languages in 2015, having a lead over well-known languages like PHP and JavaScript. Check out the source for this statistic.

I'm pretty sure that the reasons mentioned above could be sufficient for someone to move to Python as the language of choice. And, here comes my role. I will try to make such a transition as smooth as possible, by teaching you important concepts in Python in a simple manner.

Enough introduction, and let's get started! In this article, I will review (or introduce) an important concept in Python, Lists, which is necessary to know before moving forward towards more advanced topics. 

Suppose you have created a list of tasks to work on for the day. In other words, you have created a to-do list. Say that your to-do list was as follows:

  • write blog post
  • reply to email
  • read in a book

Let's write this list in Python. One way to do that is assign each to-do task to a variable, as follows:

todo1 = 'write blog post'

todo2 = 'reply to email'

todo3 = 'read in a book'

What if we had a longer list of to-dos? It is not feasible to assign each to-do a separate variable, is it? Lists here come into play. 

Python lists are considered the most versatile data type. Lists are created using square brackets [ ]. The values (items) of the list are inserted between those square brackets, and separated by a comma. So, let's see what our above to-do list would look like using lists.

todo = ['write blog post', 'reply to email', 'read in a book']

Great! But that's not all—there is more we can do with lists, as will be shown in a moment.

Accessing Items

Lists make it easy to access items regardless of the number of items you have in the list. This is done using an index. For instance, our todo list consists of three items. In Python, indexing starts from the value 0. So, the first item in the list will have index 0, the second item index 1, and so forth.

Let's say we want to access the last item in the todo list, that is 'read in a book'. This can be simply done as follows:

todo[2]

List Operations

At the end of the day, you should have finished the tasks or at least some task on your to-do list. Say that you have written a blog post. In this case, you need to remove this to-do from the list. In order to do that, you can simply use the function del. The task we want to remove is the task with index 0. So, in order to remove that item from the list, type the following:

del todo[0]

In this case, our todo list will look as follows:

todo = ['reply to email', 'read in a book']

Oh, you now realized that you would like to replace the read in a book task with read 5-pages from the book in order to be more specific, especially when you realized that this is a good practice for getting things done (GTD). All you need to do is access the index of that element, and type your new task:

todo[1] = 'read 5-pages from the book'

The todo list will now look as follows:

['reply to email', 'read 5-pages from the book']

Well, you feel you have some time for a new task, and you decided to add the task call the consultation service at the end of your todo list. This can be simply done using the append() function, as follows:

todo.append('call the consultation service')

The todo list will now look as follows:

['reply to email', 'read 5-pages from the book','call the consultation service']

Wait a minute, you remember that you had an old to-do list old_todo that you decided to combine with your current to-do list todo. Is there a way to do that, or do we have to copy and paste the items from the old list to the current one?

old_todo = ['buy grocery', 'wash car', 'borrow a book from the library']

You can simply concatenate the two lists using the + operator as follows:

new_todo = todo + old_todo

The new to-do list new_todo will now look as follows:

['reply to email', 'read 5 pages from the book', 'call the consultation service', 'buy grocery', 'wash car', 'borrow a book from the library']

Notice if you wrote new_todo = old_todo + todo, items in old_todo will come first in the new list.

We now have a nice to-do list. Let's see how many items we have so far. Simply type:

len(new_todo)

You should get six items. A reasonable number of tasks for your day, I guess.

Say that you have a very long list, and you just wanted to make sure that you have a particular task listed. Rather than manually looking for that item, you can use in as follows:

'wash car' in new_todo

If the item 'wash car' is in the list, you should get True as a return value.

List of Lists

Lists, as we have seen, are very flexible object types. The nice thing about lists is that they can contain any object type, even lists!

For instance, you can create a list that looks as follows:

complex_list =['Abder', '4.0', ['write blog post','grocery'],[['a','b','d','e','r'],['number','todo']]]

Notice that the list not only contains different object types and a list, but also a list of lists. Isn't that wonderful?

The for-loop and Lists

Lists can be used with the for-loop in an interesting way. I will show one example of this in this section. 

Say that you have the following list and you wanted to repeat each item three times:

abder = ['a','b','d','e','r']

You can do this as follows:

new_list = []

abder=['a','b','d','e','r']

for r in abder:

    new_list.append(r * 3)

The result will thus be:

['aaa', 'bbb', 'ddd', 'eee', 'rrr']

Takeaways

From this article, we can see the power, flexibility, and simplicity of Python represented in its Lists. Lists proved itself as a very versatile object type through which we can group different object types, including other Lists, and perform different operations on the List in a simple manner. For more operations on Lists, you can consult Python's documentation.

What's the thing you liked most about Lists?

Advertisement
Advertisement
Looking for something to help kick start your next project?

Envato Market has a range of items for sale to help get you started.