You can imagine a list as an horizontal bookshelf where each book represents an element.
A list is a collection of elements in an ordered sequence. These elements can be of any data type and don't necessarily have to be related.
In this article, you'll learn how to create lists and access the elements in them.
Lists are one of the built-in data structures in Python. Other built-in data structures are tuples, dictionaries and sets.
Python lists are indicated by square braces ([ ]) and the elements are separated by commas. Let's create a list of phone brands.
It's a good practice to use plural names for your list names since a list usually contains more than one element. This would distinguish the list from a variable named "phone_brand".
Let's print the list.
Accessing Elements in a List
We can access each element in a list using index. Index position in Python starts at 0, not 1.
The list name is followed by the index of the element in square braces. Let's access some elements in the list.
We can access the last element in our list by specifying the index position of the last element (6 in this case).
This method won't be so easy when we have a large list with an unknown number of elements. Python has a special syntax for accessing elements from the end of the list.
The end index starts at -1 and decreases by 1 towards the beginning of the the list.
We can also perform string methods on the string elements.
Read: String Methods
Let's compose a message with an element in the list.
List Slicing
We can access a subset of a list with slices. A list slice is indicated by a list name followed by square braces containing start and end indexes separated by a colon symbol (listname[start:end]).
Python returns a sub-list starting from the start index position up to but not including the end index position. listname[0:3] returns elements at index position 0, 1 and 2 but excluding index 3.
Let's create a list of your top 10 favorite artistes and print some sub-lists.
When the start index position is not specified, Python starts at the beginning of the list.
When the end index is omitted, Python returns all elements from the start index to the end of the list.
The above syntax allows us to print elements from any position in a list to the end of the list.
Negative values can also be used in slices. Like you saw earlier, the last element in a list can be accessed by the index -1. This index decreases towards the beginning of the list. To print the last 3 artistes without knowing the length of the list, you'll want to do this.
Third Argument in Slices
An optional third argument can be added to a list slice. It tells Python how many steps to skip. This third parameter is called a stride or step.
A stride 2 skips one element and prints the next one. A stride 3 skips two elements and prints the next one.
Strides can also be used to specify direction. A positive stride moves towards the right direction while a negative stride moves towards the left direction.
The code below prints your top 3 favorite artistes in reverse order.
The negative stride tells Python to print results from the third element to the end in the leftward direction.
List is a very powerful python feature which can be used in various ways. In the next article you'll learn how to modify lists.
Summary
- A list is a collection of elements in an ordered sequence
- Python lists are indicated by square braces ([ ]) and the elements are separated by commas
- Elements in a list can be accessed using index.
- Index position in Python starts at 0, not 1.
- Slices are used to extract sub-lists from a list
- Strides are used to skip elements. They also define the direction in which python returns elements
Very informative piece.
ReplyDelete