Lists in Python
In this Python at RabinsXP, we are going to learn everything about the lists in Python 3 such creating, indexing, and slicing, etc. We also learn about its basic list methods, nesting lists and also an introduction to list comprehensions.
What are Lists in Python?
In the last lesson of Strings in Python, we learned about the concept of the ‘sequence’. Similarly, Lists can be called a version of sequence also. But, unlike the strings, they can be altered ie. mutable which means we can change the contents of the lists.
Here we are going to learn about the followings:
- Creating Lists
- Indexing and Slicing Lists
- Basic List Methods
- Nesting Lists
- List Comprehensions (An Introduction)
In Python, a list can be constructed or created using the brackets  and commas are used to separate every element inside that particular list.
Let’s see how we can do it. For example, we have a variable name called our_list and we gonna assign some elements or a list.
our_list = [10,20,30]
- This is storing the integers only.
But we can store multiple object types like this.
our_list = ['This is a string', 50, 60.36,'r']
If you remember how we use the function
len() in the strings previously, similarly we do ‘
len(our_list)‘ to count the number of items in the sequence of the list.
Indexing and Slicing
Indexing and Slicing in Strings work in the same manner it works with Strings. Let’s learn with an example.
our_list = ['cow','dog','horse',3animals,0]
Let’s grab some elements and.
our_list-It grabs element at index 0.
our_list[1:]– It grabs index 1 and everything past it.
our_list[:3]– It grabs everything up to step 4 or index 3
We can also concatenate multiple lists with plus sign (+) like this:
our_list + ['new item'] And this doesn’t change any elements of our original list until
our_list we reassign like this:
our_list = next_list + ['new element item added'] To duplicate ‘n’ number of times we can use asterisk or multiplication symbol (*) like
our_list * 2which double the list but not permanently.
Basic List Methods
Just like in other programming languages we use arrays we can go in a similar manner with lists in Python. In practical, lists are more flexible than that of arrays. We don’t have to define any size of the list and not any particular object type.
Lets, see how we can use some of the basic list methods used in Python.
our_list = [1,2,3]– create a new list
our_list.append('I will be added.')– Using the .append() method we can permanently add the new item(s) to the end of a list.
our_list– it will show up the elements in the list.
our_list.pop(0)– using ‘pop’ u can pop off or remove the element from the list. If we don’t specify any index number like we specified(0) here then it removes or pops out the last index element.
Note: Python will throw an error if any element is not available at the index we specify. Say we don’t have
our_list here, this will give you an error.
We can also use the sort method and the reverse methods which alter out the list.
Now if we use the reverse function like this
chars_lists.reverse(), it will permanently change the elements of ‘
chars_lists‘. Now if we check the elements of chars_list, it will be
chars_list.sort() will permanently sort the list in alphabetical order and for numbers, it will sort in ascending order.
Python also supports the Nesting which means we can have data structures within the data structures. For now, we gonna say a list inside a list. Let’s learn more about this using some examples. We have some lists like below.
Now we are making a matrix of the above lists as
matrix = [l_01,l_02,l_03]. ie we have
matrix = [r,a,b,i,n,s,x,p].
Let’s grab the elements from the matrix object. Remember, we have to grab the items in the matrix object and the items inside that list.
matrix – It grabs the first item in matrix object.
matrix – It grabs the first item of the first item in the matrix object.
One of the advanced features of Python is comprehensions. It allows for the quick constructions of the lists. You need to know about the loop to understand it clearly.
Here are few examples of loop function you may like to look.
col_first = [row for row in matrix] It builds a list comprehension by deconstructing a for loop within a
Here we used list comprehension here to grab the first element of every row in the matrix object. In the next series of Python at RabinsXP, we will learn more about it.
We just learned to create lists, index and slice it and basic methods of it too. Also, we got to know about Nesting the lists basic. Along with this dive into a quick introduction to list comprehension in Python. This concludes the end of ‘Lists in Python’.
Next, in the next Python programming series (Python at RabinsXP), we will learn about Dictionaries in Python.