How to use the list() Data Structure

In Python and in most programming languages such as Java lists are mutable, which means lists are able to change. To create a list in Python is very straightforward and we can do this by doing the following:

# List created having int's, strings and float's 
my_list = [1,2,3,"Daniel", "Maia", 2.3]

You will notice you can create a list containing different data types such as int, string, and floats. As you can see its very straightforward to do, you will find in other programming languages it’s similar syntax. You will notice the list is also assigned to the variable my_list  with this you can reference the variable name across your project and access contents in that list.

If you like to play with lists, you can also create a list like, [“daniel”, “maia”, 1,2,3]  and run this in your terminal and it will display the list to you, however, you cannot reference that list unless you assign a variable to it as we did above. Like so:

Moving on…. we can create a list within a list. What do I mean by this? Well, if we take the list of our first example we can create a list of this list. This is how you would do that:

my_list_2 = [1, 2, 3, 'Daniel', 'Maia', ["Another", "List"]]

Now you might be thinking, ok well…. how do I access that second list? This brings me to my next section on accessing elements in a list. To be able to access an element in your list, all you need to do is to call you variable followed by [] so here is an example on how to access the first element in the list:

# Created list, as in our first example
my_list = [1,2,3,"Daniel", "Maia", 2.3]

# Accessing the first element in the list
my_variable[0]

In any programming language, you will find  is the first element or seen as the computers number 1. Computers start counting at number 0 not like us, human, where we start at number 0. Anyway, so the result of this you will get the first element being 1. This is the same as accessing the second list, you just need to reference the index of the second list which in this case it’s 5, however, note that when you access the second list you are accessing all the elements in that second list. Here is an example:

# Basic List
my_list_2 = [1, 2, 3, 'Daniel', 'Maia', ["Another", "List"]]

# Access the first element in the list
my_list[0]
>> 1

# Accessing the second list ["Another", "List"]
my_list[5]
>> ["Another", "List"]

# To access the second list and the first element in that list
my_list[5][0]
>> ["Another"]

Try this on your terminal if you have Python installed on your machine, if not follow the guide relevant to your operating system on one of my other blog posts.

Continuing on with lists, let’s move on to how we can add items to the list:

# Gowing lists:
favourite_things = ["raindrops on roses", "whiskers on kittens", "bright copper kettles"]

# Can a new item to the list by using the + operand as below: NOTE: This is not permanent
favourite_things + ["warm wollen mittens"]

# To make it permanent you will need to use the +=
favourite_things += ["warm wollen mittens"]

There are built-in functions within Python which will allow you do things like this. Those functions are:

  • append()
  • extend()
  • insert()
  • pop()
  • remove()

I will explain each of these in more detail:


append()

Let’s start with append(), append will append whatever value you specify to the end of the list. Here is an example:

# A simple 1 dimensional list
my_list = [1,2,3,"Daniel", "Maia", 2.3]

# To add an item with the append() method, as just an item and not another list:
my_list.append("Another item 1")

As you can see all you need to do is specify which list you want to add the elements to, so in this case my_list then followed by append(“Another item 1”) the string between the () is the element which will be added to the list. If you run this code you’re the item “Another item 1” is now added to the end of my_list list. You will see an output like so:

# The new list will be: 
[1, 2, 3, 'Daniel', 'Maia', 2.3, 'Another item 1']

extend ()

The extend function is slightly different, if you use the extend function as long as the argument is iterable then every element in the argument is then added to the list as its own element in the list, here is an example:

# Another option is the extend() function, this takes a list [] and adds it to another list as items
favourite_things.extend(["item added 2", "items added 3"])

# When you run it 'item added 2', 'items added 3' has been added to the list
# This can be done as long as the item is iterable so:
a = [1,2,3]
a.extend("abc")
# This will return [1, 2, 3, 'a', 'b', 'c']

insert()

This function allows you to enter an element at a specific index, so this example “Added to the begining og the list” is added to the beginning as the 0 index is specified

# The insert() function takes 2 arguments the index, where in the list you want the item to go and then the item itself
my_list.insert(0, "Added to the begining of the list")

pop()

Removes the item from the list but returning the value back to  you, see the examples of pop() below

# 1. Create a list
my_list = [1,2,3,4,5]

# Remove an item and return it using the index
my_list.pop(0)

# Now we can use pop() and assign it to a variable like so:
element = my_list.pop(0)
# Now we can access the element variable whenever we want but you will notice the my_list list no longer has that variable

remove()

As you can tell by the name, the remove() function removes elements in the list, however when using this function if there is more than one element that’s the same and use remove it will only remove the first instance.

remove_list = [1,2,3,1]
remove_list.remove(1)
remove_list.remove(1)
# Now it will remove all the 1's in the list

 

I hope you got to grips with how lists work in Python from this post, if not, or have questions about it please let me know and I will be happy to help. You can view all my source code here on Github

Leave a Comment