Lists in Python

List in Python is a collection of similar/different data types.

Defining a list:

A List can be defined/created by enclosing the values within square brackets '[]'.

l = [5,7,9,2,4]

The above code will create a list with the values given inside the brackets.
We can create an empty list by giving just nothing within those square brackets.

l = []

Printing a list:

We can print the list just by passing the list into the print() function.

l = [5,7,9,2,4]
print(l)
 Output: [5,7,9,2,4]

 Creating a list with predefined values:

We can create a list with predefined values by using * operator on any list multiplied by n times.

l = [0] * 5
print(l)
 Output: [0,0,0,0,0]

Accessing list elements:

We can access the elements of a list by calling the list followed by the index value enclosed in square brackets.

l = [5,7,9,2,4]
print(l[2])
 Output: 9

Since Indices of a list start with 0, Index 0 corresponds to the first element in the list, Index 1 corresponds to the second element, and so on.

We can access the last element of the list by using -1 as the index.

l = [5,7,9,2,4]
print(l[-1])
 Output: 4

Similarly, we can use -2,-3 (and so on) to access the element one before and two before the last element respectively.

l = [5,7,9,2,4]
print(l[-2])
print(l[-3])
 Output: 2
         9

We can also slice a range of elements within a list by defining the range by using a colon (:) within the square brackets. This range is returned as a list.

l = [5,7,9,2,4,1,0,6,3]
print(l[3:8])
 Output: [2,4,1,0,6]


Here the index value given after the Colon is not included in the resultant list. In the above example, only the index values from 3-7 are considered and index 8 is omitted.

Omitting the starting index for the ':' operator returns the list from the starting index to the given ending index.

l = [5,7,9,2,4,1,0,6,3]
print(l[:8])
 Output: [5,7,9,2,4,1,0,6]

Joining two or more lists together:

We can Join two or more lists in Python just by using the '+' operator.

lsta = [5,7,9,2]
lstb = [4,1,0,6,3]
print(lsta + lstb)
 Output: [5,7,9,2,4,1,0,6,3]

  Repeating a list's items:

We can repeat the items of a list by using * operator on any list multiplied by n times.

l = ['h','e','y'] * 3
print(l)
 Output: ['h','e','y','h','e','y','h','e','y']

Methods in List Class:

There are many methods in the list class which performs various tasks on the lists. Let us have a look at the most commonly used methods of the list class.

index() method:

The index() method is used to find the index/position of a given value in the list.

l = [2,4,3,9,8,7]
i = l.index(9)
print(i)
 Output: 3

If there is more than one occurrence of that value in the list, this function returns the first occurrence of the value in the list.

l = [2,4,3,9,8,7,9,1,0]
i = l.index(9)
print(i)
 Output: 3

In the above example, though 9 is present in index 3 and index 6, only the occurance at index 3 is considered by the index() function.

To get the indices of all the occurrences of the given element, we can loop through the list and append the index of every occurrence to a new list.

def allIndex(l,f)
    i = 0
    ind = []
    for element in l:
        if(element == f):
            ind.append(i)
        i+=1
        return ind

l = [2,4,3,9,8,7,9,1,0]
f = 9

print(allIndex(l,f))
 Output:[3,6]

sort() method:

The sort method is used to sort the elements present in a list.

l = [2,4,3,9,8,7,1,0]
l.sort()
print(l)
 Output:[0,1,2,3,4,7,8,9]

Similarly, we can also sort lists of alphabets.

l = ['b','a','d','c','g','f']
l.sort()
print(l)
 Output:['a','b','c','d','f','g']

This function will not work if the list contains elements of mixed data types.


Comments

Popular posts from this blog

Time Complexity of Basic functions in Python

String formatting in Python

Setting up/Installing Python