Lists In Python

Lists are ordered sequence that can hold a variety of different objects. The objects are separated by commas and are enclosed within the square braces ([ ]). List supports indexing , slicing and a variety of methods that can be called of them. Let us take a few examples to understand this.

  • A list of numbers : [ 1,2,3,4,5,6,7,8,9]
  • A list of alphabets : [ ‘a’,’b’,’c’,’d’,’e’]
  • list having multiple datatypes : [“sam” , ‘s’ , 5]

The number of objects or elements in a list can easily be found out by using the len() function. Let us understand this with an example.

In the example below, we have considered a list [1,2,3,4,5] assigned to a variable mylist and we wish to calculate the length of the list or the number of objects in it.

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

output:
5
Indexing and slicing in lists

Indexing and slicing in lists are similar to that of string . The indexing start at 0 and goes on till the length of the list – 1. Let us take an example where i will be explaining both indexing and slicing at once.(since we already did this in string section).

mylist=["abcd","efgh","ijkl"]  print(mylist[0]) #indexing  print(mylist[1]) #indexing  print(mylist[1:]) # slicing  print(mylist[1::2]) # slicing step value (jump) = 2  print(mylist[::-1]) # list in reverse order

output:
abcd  efgh  ['efgh', 'ijkl']  ['efgh']  ['ijkl', 'efgh', 'abcd'] 

Note that unlike strings , Lists are mutable . Meaning a value at a specific index of the list can be reassigned. Say there is list [‘a’,’b’,’c’] assigned to variable mylist. Now to replace ‘a’ with ‘d’ , Simply reassign the value at 0 index to ‘d’: mylist[0] = ‘d’.

Concatenation can also be performed on lists in Python that is multiple lists can be added(irrespective of the datatypes of the objects) to form a one big list. Example of this is given below.
mylist1 = [1,2,3,4]  mylist2 = [5,6,7,8]  mylist3 = [9,10]  biglist = mylist1 + mylist2 + mylist3  print(biglist)

output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Some frequently used methods in lists

When dealing with lists in Python , you must know the 4 basic operations :

  • Adding an element to a list.
  • Removing an element from a list.
  • Sorting a randomized list in a particular order.
  • Reversing the order of a list.

append() method :

The append() method is used to add a element to a list . The added element is always placed at the last index of the list. The element to be added to the list is placed within the parenthesis.Let us understand this with an example.

mylist = [1,2,3,4]  mylist.append(5)  print(mylist)  #this can also be done using concatenation by adding [5] to the list

output:
[1, 2, 3, 4, 5]

pop() method :

The pop() method returns the last element of a list (since it’s by default index is -1). However once you assign a index within the parenthesis , you will be able to grab a specific element out of the list . Let us understand this with an example.

mylist=["usa","china","russia","india"]  print(mylist.pop()) # india removed from list  print(mylist)  #now removing china out of the list  mylist.pop(1)  print(mylist)

output:
india  ['usa', 'china', 'russia']  ['usa', 'russia']

sort() method :

The sort() method is used for sorting a set of randomly arranged objects . sort() is an inplace method however it won’t return anything. In the example below i have a list [1,2,4,3] assigned to mylist. I will be performing the sorting operation in both the direction( from ascending to descending and from descending to ascending) . The default direction is from descending to ascending however that can be changed by assigning the reverse value within the parenthesis ( it can be True or False).

mylist = [1,2,4,3]  print(mylist.sort())  mylist.sort(reverse = True)  print(mylist)

output:
None  [4, 3, 2, 1]

Note that second line of the code prints ‘None’ on the screen because the .sort() method does not have a return value/type.

reverse() method :

The reverse() method is also an inplace method and does not return anything so you will have to call the list again to see the result.