Lists

Briefly, we said that Lists can contain more than one data type, are shown with square brackets and are mutable. Now let’s show Lists with an example.

list =[9, "Python", 7.2, [8, 1], ("PHP", 5, 'b')]

As you can see above, we created a List named list.

Well, if you ask how we can reach the elements of the list, let’s show it right away.

Accessing Lists Elements

list[2]
7.2

As you can see, it is very simple, let’s show another example right away.

list[4]
("PHP", 5, 'b')

As you can see, our element in the 4th index is a tuple and to access their item is simple, let’s show another example right away.

list[4][0]
PHP

Well, if you ask how I can reach more than one item, let’s show it right away.

List[start, end, increase amount]

list[0:2]
[9, 'Python']

If you’ve noticed, it took the indexes of the elements by one increment by default, and took the zero and one elements together, not the 2nd element, because it doesn’t include the 2nd element.

Now that we have learned what Lists are, and how it is used, and how to access the elements of the List, let’s move on to the List’s Methods.

List Methods

Before moving on to the Methods of the List, let’s talk about the concept of Method. Method is on something; Adding, removing, changing etc. So let’s take a quick look at what the List’s Methods are. dir() function could see all the methods. Let’s show it now;

dir(list) 

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
 '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', 
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__',
 '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__',
 '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index',
 'insert', 'pop', 'remove', 'reverse', 'sort']

We got an output as __xxx__ methods, since they are private methods, we will not process them for now.

We will cover these methods;

['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert',
 'pop', 'remove', 'reverse', 'sort']

Append Method

It is the method for adding a new element to the list. With this method, we can add only one element to the List, and we can add only one element to the end of the List.

Its usage is as follows;

fruits = ["apple", "peach", "cherry"]
fruits.append("watermelon")

The new version of the list will be as follows;

['apple', 'peach', 'cherry, 'watermelon']

Insert Method

We were able to add elements only to the end of the list with the append method, but you can add an element to the desired index of the list with the insert method, and its usage is as follows;

fruits.insert(2, "banana")

The new version of the list is as follows, the “banana” element has been added to the second index of the list.

['apple', 'peach', 'banana', 'cherry, 'watermelon']

Extend Method

Let’s create a second list first;

fruits = ["apple", "peach", "banana", "cherry", "watermelon"]
fruits2 =["orange", "peach"]
fruits.extend(fruits2)
 
print(fruits)
 
['apple', 'peach', 'banana', 'cherry', 'watermelon', 'orange', 'peach']

Remove Method

fruits.remove("watermelon")
print(fruits)

['apple', 'peach', 'banana', 'cherry']

In this way we deleted the “watermelon” element from the list of fruits.

Pop Method

This method is for deleting elements like the remove method. And this method does the deletion with the index of the element, not the element, and when you do not specify an index, it deletes the last element of the list by default. Also, this method writes the deleted element to the screen.

>>> fruits = ['apple', 'peach', 'banana', 'cherry', 'watermelon', 'orange', 'peach']
>>> fruits.pop()
'peach'

Sort Method

Let’s illustrate the use of this method, which is for sorting the elements of the list alphabetically, with an example.

fruits = ["apple", "peach", "banana", "cherry", "watermelon"]
fruits.sort()
print(fruits)

['apple', 'banana', 'cherry', 'peach', 'watermelon']

Reverse Method

Unlike the sort method, this method reverses the elements in the list, let’s show it with an example;

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

As you can see, if we write the elements in reverse and use them again, our list will be restored.

Index Method

This method allows us to search for the elements in the list, let’s show it with an example;

fruits = ["apple", "peach", "banana", "cherry", "watermelon"]
fruits.index("apple")
0

Count Method

This method, on the other hand, is useful for finding how many of the queried elements are in the list, let’s show it with an example;

list = ["apple", "peach", "apple", "cherry", "watermelon", "grape", "apple"]
list.count("peach")
1
list.count("apple")
3

Copy Method

This method is useful for copying a list. Let’s show the usage right away.

list1 = ["apple", "peach", "cherry", "watermelon", "grape"]
list2 = []
list2 = list1.copy()
print(list2)
 
['apple', 'peach', 'cherry', 'watermelon', 'grape']

Clear Method

This method does the delete function, but there is one thing you should not confuse. This method is used to empty the list, not the unset list.

list =["apple", "peach", "cherry", "watermelon", "grape"]
list.clear()
[]