Python for Beginners — Lists

Armindo Cachada
The Startup
Published in
3 min readSep 13, 2020
Photo by Glenn Carstens-Peters on Unsplash

Lists are the single most used data structure in Python. It is crucial that you learn it well, as lists are used pretty much everywhere in Python programs that you will come across. If there is a topic in Python you will want to spend time on, List is the ONE

What is a list?

A list is a special data structure in Python that is used to store collections in a determined order. A list can change over time(mutable). Python lists are written using square brackets notation. Like everything else in Python, a list is an object.

How to create a list

Let’s look at some examples on how you can define a list

Creates a list of integers/numbers:

list1 = [1,2,3,4]

Creates a list of strings:

list2 = [“one”,”two”,”three”]

Creates a list of strings/integers:

list3 = [“one”, 1, “two”, 2, “three”, 3]

Creates a list of lists:

list4 = [["one","two","three","four"],[1,2,3,4,5]]

As you probably have realized by now, a list in python is a very flexible data type. Unlike more strict languages Python allows free for all with lists.

How to access elements in a list

When you create a list in Python, elements in the list are numbered starting from index 0. To access elements in a list you can use the index if you know where your elements are located. For example:

element = list3[0]
print(element)
>one

Or, if you want the last element:

element = list3[5]
print(element)
>3

If you want to get the last element without counting(can get quite tedious), you can also do:

element = list4[-1]
print(element)
>3

Nice isn’t it?

You could also want the second last item and so on:

element = list5[-2]
print(element)
>three

You can do so much more useful things with Lists, using slicing. Like the name suggests you can take slices out of the list using a special pythonic notation. You got to love it!

Without wanting to steal from our next lesson, let me just give you an example:

elements = list1[1:3]
print(elements)
>[2,3]

Using this type of notation you need to specify the start index and the stop index. The result will be a sublist containing elements starting with index start and ending with index stop -1.

Rest assured that we will dedicate a lesson to slicing as it deserves.

How to replace Elements in a List

You can change the elements in a list easily.

For example

list1[0]="one"
print(list1)
>["one",2,3,4]

You can replace more than one element at the same time as well

list2[0:2]=[1,2]
print(list2)
>[1,2,"three"]

How To Add Items to a List

To add items to a list, you can use the append() method

list2.append("four")
print(list2)
>[1,2,"three","four"]

Likewise, you can remove elements from a list using the remove() method

list2.remove("four")

The remove method removes the first occurrence of the element given as a parameter from the list.

There are other ways in which you can delete elements from a list. For example the pop() method or the del() method. At this point it will be a good exercise to consult the python API documentation as you can see a comprehensive list of methods that you can use in a List.

How to loop over elements in a List

Now, we get to the meat of lists. Lists are especially useful for looping. You can iterate over a list in a variety of ways.

Here is the simplest:

list=[1,2,3,4,5]
for element in list:
print(element)
>1
>2
>3
>4
>5

Also, you can iterate over lists using List Comprehensions.

list=[1,2,3,4,5]
list_comprehension = [ element + "," for element in list ]
print(list_compreheension)

We will cover List comprehensions in a separate lesson!

There is so much more to learn about lists. So don’t stop here in your reading about Python lists.

--

--