Skip to main content

Python crash course: List


Lists in Python 

This is the fifth article of the Python crash course. In the previous article we have discussed the concept of strings in python, we've also covered string formatting in python. In this article we will discuss the concept of lists in python. We are already familiar with the sequences(in strings).

In this section we will learn about:

1.) How to create lists
2.) How to use indexing and slicing in lists
3.) Inbuilt list methods
4.) Nesting in python lists
5.) Intro to List Comprehensions

How to create lists

In [1]:
# Lets create a variable with the name of list_one
list_one = [1, 2, 3, 4]

In the above cell we have created a list of integers, but it is upto you that what type of objects you fill in the list you created.

Lets take an example:
In [2]:
list_one = ['Ankush Pandey', 'a', 23, 3.5, 'bob']

We have already discussed the use of len method, we can use len method in this case also to get the total number of elements we have in the list, for example let's print the length of the above defined list.

In [3]:
print(len(list_one))
5

List indexing and slicing

As we have discussed in the previous article, the concept of slicing is very much similar to string.

In [4]:
list_one = ['Ankush Pandey', 'a', 23, 3.5, 'bob']
In [5]:
# print the elements at various indices.
list_one[3]
Out[5]:
3.5
In [6]:
# print elements starting from element at index two and going till the end
list_one[2:]
Out[6]:
[23, 3.5, 'bob']
In [7]:
# print elements starting from element at index zero and going till the fourth element
list_one[:4]
Out[7]:
['Ankush Pandey', 'a', 23, 3.5]

Just like the strings we can concatenate two lists using the '+' operator

In [8]:
list_one + ['new name', 568, 13.5]
Out[8]:
['Ankush Pandey', 'a', 23, 3.5, 'bob', 'new name', 568, 13.5]

The above cell didn't change the actual list

In [9]:
list_one
Out[9]:
['Ankush Pandey', 'a', 23, 3.5, 'bob']

Now let's update the list

In [10]:
# reassignment to update the original list

list_one = list_one + ['new name', 568, 13.5]
In [11]:
print(list_one)
['Ankush Pandey', 'a', 23, 3.5, 'bob', 'new name', 568, 13.5]

We can also use the * for a multiplication method similar to strings:

In [12]:
# Now lets multiplicate the original list
print(list_one * 2)
['Ankush Pandey', 'a', 23, 3.5, 'bob', 'new name', 568, 13.5, 'Ankush Pandey', 'a', 23, 3.5, 'bob', 'new name', 568, 13.5]
In [13]:
# The original list is not altered
print(list_one)
['Ankush Pandey', 'a', 23, 3.5, 'bob', 'new name', 568, 13.5]

Inbuilt list methods

Lists might sound like the arrays in other programming languages, a sequence of sumbers, but they are cirtainly not. Because unlike the arrays lists dont have restrictions to store the same data type. Let's look at some of the methods of list.

In [14]:
# let's create another new list
list_1 = ['a', 4, 7, 5, 'd']

append() method: to add items in the last of the list.

In [15]:
list_1.append('first item')
In [16]:
list_1
Out[16]:
['a', 4, 7, 5, 'd', 'first item']

pop() method: pop method is just opposit of the append method, it is used to pop items from the list. By default it removes items from the last of the list and return it, but when passed with the index it can remove any specified element and return it.

In [17]:
# Let's pop the element at the index 2
list_1.pop(2)
Out[17]:
7
In [18]:
list_1
Out[18]:
['a', 4, 5, 'd', 'first item']
In [19]:
# Let's save the poped item in a variable and display it
popped_item = list_1.pop()
In [20]:
popped_item
Out[20]:
'first item'
In [21]:
list_1
Out[21]:
['a', 4, 5, 'd']

If we try to access unknown index (index more than the capacity of the list) it will show an error.

In [22]:
list_1[555]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-22-6f161eff4626> in <module>
----> 1 list_1[555]

IndexError: list index out of range

sort() method is used to sort the list.

reverse() method is used to reverse the list

It is clear that the method names in python programming are quite convincing and meaningful.

In [ ]:
list_2 = ['e', 'a', 'u', 'o', 'i']
In [ ]:
list_2
In [ ]:
# now let's sort the list
list_2.sort()
In [ ]:
list_2
In [ ]:
# now let's use the reverse() method to reverse the list
list_2.reverse()
In [ ]:
list_2

Nesting in Python lists

Now let's create an anology of 2D arrays using python lists. I am using the word analogy because lists in python are very different than array in any other language.

So, let's use nesting to create 2D arrays

In [ ]:
list_1=[9, 8, 6, 4]
list_2=[3, 2, 4, 1]
list_3=[9, 6, 8, 7]

# Now we will cfreate a list of lists so lets see that
list_2d = [list_1, list_2, list_3]
In [ ]:
print(list_2d)

Till now we have used single level of indexing to access any element in a 1D list, but now in the case of nested lists we will have to use multilevel indexing to access any element.

In [ ]:
# Let's see what is the first element in the list
list_2d[0]
In [ ]:
# Now we will access the first element of the first element of the list
list_2d[0][0]

Intro to list Comprehensions

Now let's take a very quick look at the concept of list comprehension. This is a feature of Python language where you can create lists along with performing various operations simultaneously, very easily.

We will cover this topic in a separate article, till then. Let's take an example of list comprehension.

In [ ]:
# Lets create a list using for loop
comp_list = [row[0] for row in list_2d]
In [ ]:
comp_list

If you are not able to understand the comprehension topic, don't panic we will cover comprehension in a separate article.

Thankyou

Comments

Post a Comment

RUNTIME