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]
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))
5
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
abcd
efgh
['efgh', 'ijkl']
['efgh']
['ijkl', 'efgh', 'abcd']
mylist1 = [1,2,3,4]
mylist2 = [5,6,7,8]
mylist3 = [9,10]
biglist = mylist1 + mylist2 + mylist3
print(biglist)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
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
[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)
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)
None
[4, 3, 2, 1]
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.