Blog

Python Lists with Examples

  • (4.0)

Introduction:

Python is a powerful programming language that has started regaining its fame for its usage in the Data Science along with the latest technologies like R and etc. Having said that, let us take a look at the tiny winy bits of concepts to get ourselves stronger in this programming language.

In this article, we will try to learn about the lists available in Python programming language. To provide you the gist of what lists can do, please take a look at the following section.

Python lists:

Lists is one out of the four very important constructs provided by Python programming language. The rest of the constructs available are tuples, dictionaries and sets. With lists, we achieve saving and storing an ordered collection of items which can generally be different but should not be so, for various purposes. The elements that are contained within a list are separated by commas and are made available inside of square brackets. A sample list in Python can be declared as follows:

    characters = [‘Cersei’, ‘Missandei’, ‘Danerys’]

Having that said, we can define a list inside another list – just like the following:

     knights = [‘Jon’, ‘Jamie’, ‘Bron’, [‘Cersei’, ‘Missandei’, ‘Danerys’]]

Python saves lists as ordered collection of items or objects, we can safely term them as sequence types – as they behave just like a sequence. In simpler words, a sequence in the Python’s context is what we can iterate over. Hence lists, strings, tuples and sets are all called as “iterables”.

Interested in mastering Python Training?
Enroll now for FREE demo on Python Training.

Now with that being discussed, let us see how we can access elements out of these lists that we have created earlier. We can access elements of a list by the index operator, considering the example above – there are 3 elements in it and the index ranges from 0 to 2. Hence, if we are referring to an index out of these then we will get an IndexError. A point to note here is that the index is always an integer and not a float or decimal, usage of such indexes would throw a TypeError.

Related Page: String Formatting - Python

Now let’s look at an example:

Python Characters

The example above, has used a little knowledge on the concept of lists that we have acquired as of now and also has applied a little knowledge of the for loop construct provided by the Python programming language. Basically, the example above has 3 elements within the list and accessing the list was performed using the index of the elements within the list. The xrange method has provided the indexes 0,1,2 and hence we were able to access the individual elements of the list that was created in the example above.

Related Page: Classes And Objects - Python

There is an interesting tip that we can learn here, which is about the Negative indexing provided by the Python programming language. Generally a negative index is interpreted as the nth element of the list from the end. If we were to apply this to our example above, this is how it is going to be:

Python Negative Indexing

In the examples above, we have accessed the elements either from the beginning or from the end. With the following example, we are going to introduce you the next feature provided by Python programming language – slicing operator :. Let us take a look at the following example now.

Python Slicing Operator

 

Closely observe each usage of the slicing operator “:”, to understand it better.

The first example is a simple one that says, print me the elements of the list named characters with the start index as 1 through 4 – namely 3 elements of the list. That’s exactly is the output of the first command.

Now if you carefully observe the second example, this says, print me just the elements from the beginning until the 5th last element of the list. Since the list had only 6 elements and the command was to print the elements starting from the beginning until the 5th last element, it would obviously display the only one element of the list.

Now the third example tells to print the elements of the list starting from the index 4 until the end of the list. This is exactly what has been displayed in the output for the third example.

Observing the behavior in the last three exmaples, you might have understood the usage of the split operator. Hence when we have not given any indexes from the beginning or to the end, the fourth command has returned all the 6 elements of the list.

Learn more about Python Interview Questions in this blog post.

Now getting on to the next topic of adding or changing the elements of a list. Elements can be added or changed on a list, as a list is MUTABLE as against a string or as a tuple (both of these are IMMUTABLE, meaning, cannot be changed). Let us now look at an example to understand this even better.


Python Mutable Characters

What have we observed on the first command? We have referred to the element with index 0 and assigned a new value of ‘a’ instead of ‘A’ to the list named characters and hence the output.

The execution of the next command is very similar to what we have seen in the first one, but the only difference is that we can modify the contents of the elements with indexes 1, 2 and 3 with newer values of ‘b’, ‘c’ and ‘d’ instead of ‘B’, ‘C’ and ‘D’. Once these are modified and when printed, you would see that the change is persisted to the list and hence the output of second command as well.

Now that we have understood how to update a list with newer values, let us take a look at how we can add new elements to our list.


Python Append

The append() function adds an element to the list provided with the new one.

The extend() function adds the number of elements provided as input to it to the existing list and hence the output of both the commands, adding elements to the list at the end of the list.

Now that we have understood updating and adding elements to a list, let us look at an example of removing or popping elements from a given list.

From the example below, remove(‘A’) removes the element from the list directly. The method pop(), if given an index then it will pop the element of the list as per the index provided and if there is no index value provided to this method, then it will pop the right most element of the list, hence the output of all the three commands based on the explanation given here.

Python Elements

Conclusion:

In this article, we have seen what lists are and why are they used in Python programming language. We have also tried to take a deeper look into the concept with various examples as well.

Hope that you were clear with the concepts after going through this detailed article, please do comment if you have any suggestions to make.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules