Lists

Lists, as the name suggests, is a list of anything. This could be a list of numbers, a list of alphabets, list of some random characters. There could be a lot achieved with lists and I will be guiding you through some of the general functions.

We can create a list by wrapping the square brackets([]) around the elements and each element in a list is separated by a comma.

Here are some examples of valid lists:

numbers = [1,2,3,4,5]
names = ['Bob','Mary','Misty']

Here is how invalid lists look like:

numbers = [1 2 3 4 5]
names = ['Bob' 'Mary' 'Misty']

Now that we know how we can create a list, let us jump on into basics. Sometimes, we would want to know the exact position of a character in a list for coding programs. For example, we want to access the 4th term in numbers(The number we are talking about is 4). In our language we know what the 4th term is but Python can not understand our human language and for Python to understand what term we are talking about, we use the term index.

Now, index is the numbering of each character inside a list. We have learned to access a specific character in a string using the index in the for loop and the in operator. Here is a glimpse:

An index in python starts from 0. So what we call the first term in our language, is the 0th term in Pythonic language you could say. So, the 2nd term in our language is the 1st term in the index language. 3rd term is the 2nd term so on and so forth.

Now we know what indices are but how do we access the specific index in a list? To do this, we use the square bracket operator and then we type in the index number we want to access inside these brackets.

numbers = [1,2,3,4,5]

The first 0th index would be 1, 1st index would be 2, 2nd index would be 3 and so on.

If we want to access the number 3, we would first want to check the index position. Looking at it, we know that the position of 3 is the 2nd index and inorder to print it out, we simply type:

print(numbers[2])


Now that we know the basics, let us try some list functions.

Finding the total length of a string:

len() is used to find the length of a list. We first define a list and we put the name of the list inside the parenthesis or one could just put the raw list inside the parenthesis.

numbers = [1,2,3,4,5]
len(numbers)

or

len([1,2,3,4,5])

Because we have 5 elements inside the list, our output would return 5

Append:

When we have a list, we could either add new elements to it or delete old elements. To add new elements, we use the append() function. Append takes in 1 argument inside the parenthesis and this states the element we want to add to the list. But always remember that when we append something to a list, it is always added to the end of the list.

Syntax:

Let us again assume a list with numbers up until 5 and we would want to append the number 6 onto the list.

numbers = [1,2,3,45]
numbers.append(6)

We have '.' after our list's name. This specifies that what ever functions we use after the '.' is only applicable to the name we have mentioned.

Now try adding some of your own elements on to your list.

Reverse a list

We can always reverse a list in python. All we have to do is call the reverse(0 function.

numbers = [1,2,3,4,5]
numbers.reverse()

Sorting

In python, we can always sort the list. This could be either the numbers or alphabets. Remember that when we call the sort function, the lists always sorted in a ascending order but we could do a descending order too and there are 2 ways you could do this.

numbers = [4,5,3,6,13,4]
numbers.sort() 

This sorts the list in ascending order. Here is how we do it in descending order.

numbers = [1,2,3,4,5]
numbers.sort(reverse=True)

or we could first sort the list in ascending order and then use the reverse() function to get our list in descending order.

Find the maximum and minimum

We use max() to find the maximum value and min() to find the minimum value.