HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

2) More Data Types Lesson

What is a Python List

9 min to complete · By Martin Breuss

Lists are an extremely common data type in Python. You'll likely use them all the time in your programming work. Just like tuples, lists are sequences of any other data type.

You can create lists of strings, lists of strings and numbers, lists of tuples and integers, and even lists containing more lists, each of which contains a string with the value "inception"... You get the point!

However, unlike tuples, lists are not carved in stone. You can change the elements in a list, you can add to the same element without needing to create a new one, as well as remove items from it. You'll start by creating a new list that you'll work with for the rest of this lesson.

Creating Lists

There are two different ways to create a list in Python:

  1. Using square brackets ([]) around elements that are separated by commas (,)
  2. Using list() on a sequence of elements that can be converted to a list

The code snippet below will show you both of these ways of creating a list:

# Square Brackets
list1 = [1, 42, "hello"]
print(list1)  # OUTPUT: [1, 42, "hello"]

# List Function
tup = (1, 42, "hello")
list2 = list(tup)
print(list2)  # OUTPUT: [1, 42, "hello"]

Using list() like in the example above is a type conversion similar to the ones you have seen using str() and int(). To create a list from scratch, you'll generally use the square brackets.

Lists Are Usually Homogenous

As you can see in the code snippet above, lists seem very similar to tuples so far. They are sequences of elements, where the elements can be whatever you want.

However, while it is possible and not a mistake at all to combine different types of elements into a list in Python, you usually want to keep your lists homogenous.

Colorful illustration of a light bulb

Info: Many programming languages enforce the data type of all the elements in a sequence so that you wouldn't be able to mix them in the first place. Python is more chill about it, but that doesn't mean that you need to abuse the freedom that Python gives you! Often, it can be helpful to apply some limitations, even if they aren't enforced.

That means that you should avoid mixing data types inside a list and instead only use elements of one data type, e.g., only integers or only strings, in a single list.

Colorful illustration of a light bulb

Info: When you feel you need to combine different data types in one sequence, it's often better to use a tuple instead. The fact that tuples are immutable makes it less likely that you'll end up with unexpected bugs.

In the code snippet below, you'll create a homogenous list that contains only strings, a bucket list of things a person might want to do in their life:

bucket_list = ["climb Mt. Everest", "eat fruits from a tree"]

If you don't relate to these life events, feel free to change the string elements in your own bucket_list. Next, you'll revisit how you can pick out a single element from a list.

Lists Elements Have Indices

Just like tuples and strings, list elements have indices that allow you to access each element by its unique index position. Lists support indexing through the same syntax that you've already gotten to know:

bucket_list = ["climb Mt. Everest", "eat fruits from a tree"]
print(bucket_list[0])  # OUTPUT: "climb Mt. Everest"

You can also use the familiar square-bracket notation to create slices of your lists or access them in steps.

List Practise

  • Revisit the lesson on string slicing and apply the concepts to your list.
  • Access the last element in a list using negative indexing.
  • Create a longer bucket list with at least six items. Then, use slicing and steps to pick out only every second element in the list. Don't use a loop for this!

As you can see, both indexing and slicing work the same on lists as they do on the other data types you've already gotten to know. Since lists are sequences, there is yet another aspect that works the same as with these data types.

Lists Support Iteration

You can iterate over a list using loops in the same way you did with strings and tuples:

for task in bucket_list:
    print(task)

Of course, there are more interesting things you can do with each list item than just printing it out, but for now, you're just learning about the basic concepts, and you are always encouraged to go ahead and play around with the concepts with your own ideas :)

In fact, iterating over lists to perform operations on each item is a powerful and common operation that can help you achieve a lot of tasks in programming. Python's for loop makes this iteration intuitive.

Loops Refresher

  • Refresh your understanding of how to use Python for loops by revisiting the lesson on loops in the previous module.
Colorful illustration of a light bulb

Additional Resources

  • Official Python Tutorial: Lists

Summary: What is a Python List

  • Lists are sequences of elements that can be of any data type. However, it is often useful to keep lists homogenous and only consist of elements of the same data type.
  • You can index list elements, create slices, and iterate over lists using the same syntax as for strings and tuples.
  • After revisiting how lists are the same as these other data types, you are now ready to find out how lists are different.

Two Methods for Creating Lists

  • Using square brackets ([]) around elements that are separated by commas (,)
  • Using list() on a sequence of elements that can be converted to a list