Python Lists || Python Tutorial || Learn Python Programming

Toggle fullscreen Fullscreen button

Sharing buttons:

Order.. matters.. And in Python, lists make it easy to work with ordered data. Lists are

a built-in data structure for storing and accessing objects which belong in a specific

sequence. We will now learn how to create and use lists, and we will do so in a linear

and orderly fashion...

There are two ways to create a list. One way

is to use the list constructor. But a simpler and more common way is to use brackets.

When creating a list, you can also pre-populate it with values. For example, let’s create

a list with the first few prime numbers.

You can always add values later by using the “append” method which allows you to add

new values to the end of the list. Let’s append the next two prime numbers: 17… and


If you display the list, you will see it contains the new values. Notice how lists preserve

the order of the data - this is different from sets. In sets, the order is not important.

In lists, order is everything. You do not have to view the entire list. If

you want to see a specific value, you can access it by its index. In computer science,

you start counting with 0, not 1. So the elements in our list “primes” are indexed 0.. 1..

2.. 3.. 4.. 5.. 6.. and 7.. To view the first item, you type the name

of the list, and the index in brackets. The first item is 2.

The second item has index 1, and the second item is 3.

And so on…

Notice how the indices increase by one as you go from left to right.