Methods in Python Lists

Methods in Python Lists

Hi Friends……Welcome again to our session on Methods in Python Lists. In the previous session, we had a detailed understanding about Python Lists. We will extend this session now to see how we can make use of the lists and some pre-defined methods and functions to be used with Python Lists.

Lets start our session on Methods in Python Lists:

1. sort method on integers – Python Lists provides us with an in-build method to sort the list. Instead of sorting the list by manually comparing list elements one by one, we can directly make use of sort function.

list = [10,5,67,34,7]
print("Actualy list is:",list)
list.sort()
print("Sorted list is:",list)

2. sort method on strings – try this method on list having alphabets and strings. Clearly the logic behind sorting is based on the ASCII values of the alphabets

list = ["z","Z","xYz","f","b","a"]
print("Actualy list is:",list)
list.sort()
print("Sorted list is:",list)

3. sort method – note that we cannot use this method on combination of integers and alphabets/strings. Else we will see error because we cannot compare integers and alphabets

list = ["z",1]
print("Actualy list is:",list)
list.sort()
print("Sorted list is:",list)

4. reverse method – we can use the reverse method to just reverse the indexes of the list elements. We can use integers, alphabets or combination also with this method.

list = ["z",1,2,"A"]
print("Actualy list is:",list)
list.reverse()
print("Reversed list is:",list)

Assigning one Python List to another:

I hope above two methods are clear. Now before going to more methods, lets try to understand some basic concepts. Imagine a variable below:

var1=5
Now imagine we assigned another variable with first variable: var2 = var1. This means var2 = 5. Now if we change var1 =10, var2 is still 5.

var1 = 5
var2 = var1(var2=5)
var1 = 10

This means that name of an ordinary variable equals to its value. Or we can say that the variable name  equals to its content(var1 = 5). Same way var2 when assigned with var1 means that var2 equals to 5. Hence when we change var1 = 10 does not impact var2.

But when it comes to list, the name of the list equals to the memory location where the list is stored. So, by that way, imagine we have list1 = [1,2] and we assign list2 = list1, then list2 is also assigned with the same memory location as that of lis1. Now if we change list1 elements, it will impact list2 also.

list1 = [1,2]
list2 = list1
list1[0] = 10
print(list2[0]) – this will print 10

This means that list1 variable name connects to the memory location where list is stored. Hence if list2 is assigned with list1 and list1 is changed, then list2 is also changed.

5. List assignment – let’s just see one example on above concept. Changing element of list1 changes the elements of list2 also

list1 = [1,2,3]
list2 = list1
print("First list is:", list1)
print("Second list is:", list2)

list1[2] = 10
print("First list is:", list1)
print("Second list is:", list2)

6. List slicing/list elements copying – There is a solution to the above concept. In case we want to just copy the elements of the list1 to list2 and not memory location, then we can use list slicing by using list2 = list1[:]. That ways, if we change list1 elements, list2 still remains same.

list1 = [1,2,3]
list2 = list1[:]
print("First list is:", list1)
print("Second list is:", list2)

list1[2] = 10
print("First list is:", list1)
print("Second list is:", list2)

7. List slicing based on indexing – there is another way of slicing list by using the index values of the list. We can use list1[start:end]. Here elements starting from index value ‘start’ till index value ‘end-1’ will be copied.

list1 = [1,2,3,4,5]
list2 = list1[1:4]
print("First list is:", list1)
print("Second list is:", list2)

list1[2] = 10
print("First list is:", list1)
print("Second list is:", list2)

8. List slicing using negative indexing – Negative indexing also works the same way as above. Here list1[1:-2] means it will start from index 1 till index -3.

list1 = [1,2,3,4,5]
list2 = list1[1:-2]
print("First list is:", list1)
print("Second list is:", list2)

list1[2] = 10
print("First list is:", list1)
print("Second list is:", list2)

9. List slicing in opposite direction – if we try to slice the list in which start element is on the right side of the end element, then list will be empty. This means that list cannot move in opposite direction.

list1 = [1,2,3,4,5]
list2 = list1[-1:2]
print("First list is:", list1)
print("Second list is:", list2)

list1[2] = 10
print("First list is:", list1)
print("Second list is:", list2)

10. List slicing by omitting start/end value – if while slicing, we omit start index value, then its assumed that slicing will start from first element. Similarly, if we omit end index value, then slicing will be done till last element

list1 = [1,2,3,4,5]
list2 = list1[:2]
list3 = list1[3:]
print("First list is:", list1)
print("Second list is:", list2)
print("Third list is:", list3)

11. Deleting list elements by using slicing – we can delete list elements by using slicing just like above – del list[start:end]. This will delete list elements from start till end-1. It will not create a new list, just update the existing list

list1 = [1,2,3,4,5]
print("Actual list is:",list1)
del list1[2:4]
print("Updated list is:",list1)

12. Deleting all list elements – if we omit start and end values, then all list elements are deleted, and list becomes empty. Last print statement will print empty list.

list1 = [1,2,3,4,5]
print("Actual list is:",list1)
del list1[:]
print("Updated list is:",list1)

13. Deleting the list – if we just use the list name while deleting, then the list is deleted. Last print statement will throw an error since list is removed now.

list1 = [1,2,3,4,5]
print("Actual list is:",list1)
del list1
print("Updated list is:",list1)

14. Deleting one list – when we assign one list to another, then they both point to same memory location. Updating one list will update second one also. However, when we delete one list, then only the memory link pointed by one list is broker, the second list remains intact. See below, when we updated l1, l2 is also updated. But when l1 is deleted, l2 still exists.

l1=[1,2,3]
l2=l1
print(l1)
print(l2)
l1[0]=10
print(l1)
print(l2)
del l1
print(l2)

15. List elements check using in/not in operators – we can check whether some element is present in the list or not. The output will be True or False.

list1 = [1,2,3,4,5,"a"]
var = 5
print(1 in list1) --return True
print(10 in list1) --return False
print(15 not in list1) --return True
print(var not in list1) --return False
print("a" in list1) --return True

Previous Session on Python Lists: Python Lists – Use and Features – Tech Tutorials (mytechdevice.com)

More posts on Python: Amit Mathur | Python Programming Blog

Leave a Reply

Your email address will not be published.