Python Lists

Hello to all! In the last post, we got to know the basic data types. And today we’ll look at lists in Python in more detail.

In general, lists can be represented as a sort of ordered collection of objects whose types do not matter. You can imagine any word as the simplest list. For example, the word “Example”. The square brackets [] are used to indicate the list, after which the body of the list goes:

In more detail, the code can be written like this:
list = [‘Example’] – list – list name, [] – list designation, ‘Example’ – list contents
list2 = [1, 2] – list2 – list name, [] – list designation, 1, 2 – list contents
print (list) – displays the list list on the screen
print (list2) – displays list2 on the screen

The list can also be created programmatically. To do this, use a loop inside square brackets. An example code looks like this:

list = [i * 2 for i in range (1,10)] – the created list list populated with the loop i * 2 for i in range (1,10)]. i is an iterator multiplied by two for position i in the range from 1 to 10 (the range () function is used. Most often it is used in for loops. The function itself can take from one to three arguments, but the arguments can only be integers)
print (list) – output of the generated list

Clear business – the created list needs to be processed somehow. So – it’s time to get acquainted with the basic functions and methods of lists.

Important: all list methods described below, unlike the methods related to strings (for more details here), change the list itself, which means that the execution result does not need to be written to a variable attached to the list.

To begin with – let’s try to display each element of the list on a separate line. To do this, use a loop in the form: for x in list, where x is an iterator, list is the name of the list:

In more detail, the code is as follows:
list = [1, 2, 3, 4, 5] – the list itself
for x in list: – list iteration cycle
print (x) – display element on screen

list.append (x) – Add an x to the end of the list. In practice, the code is as follows:

and if you consider it in more detail, then:
list = [1, 2, 4] – the list itself
print (list) – list output to the screen
list.append (5) – we connect the append method and thus add the number 5 to the list (list name). Important: the append method adds to the end of the list
print (list) – displays the list list on the screen. As you can see – at the end the number 5 appeared

list.extend (list2) – adds all the elements of list2 to the end of the list list. The working code example itself is below:

Similarly with the previous code, let’s parse this one:
list = [1, 2, 3] – list 1
list2 = [4, 5, 6] – list 2
print (list) – display list 1
print (list2) – display list 2
list.extend (list2) – we connect the method of adding data from list 2 to the end of list 1
print (list) – print the contents of list 1. As you can see, the data from list 2 appeared at the end

list.insert (i, x) – this method allows you to put the value x at position i. In the form of code, this can be represented as follows:

list = [1, 2, 4] – list list
print (list) – display the list on the screen
list.insert (2, 3) – we connect the insert method, which adds the value 3 to position 2. Here it is important to remember that the countdown in Python starts from zero).
print (list) – displays the list list.

list.remove (x) – delete the first element in the list whose value is x. It is important to know that if there is no such element in the list, we get a ValueError:

list.pop ([i]) – delete the item at position i and return it. If i is not specified, delete the last element:

Here we consider the code in more detail:
list = [1, 2, 3, 4, 5] – the list itself
print (list) – display the list on the screen
list.pop () – deleted the last item in the list (after all, the index is not specified!)
print (list) – printed the list. Apparently – the last element is really deleted.
list.pop (2) – delete the second item in the list.
print (list) – print the list. And the truth is that the second element has been deleted.

list.index (x) – displays the position number in the list of x element:

list.count (x) – returns the number of list items with a value of x:

As seen from the code:
list = [‘apple’, ‘apricot’, ‘pear’] – there is a list of three elements
print (list) – display a list
print (list.count (‘apple’)) – we connect the function of counting the number of elements in the list. Apparently – the apple in the list occurs only once.

list.sort ([key = function]) – sort the list based on the required function

list.reverse () – a method to expand the list:

list.copy () – method to create a shallow copy of the list:

list.clear () – a method to clear the list. There was a list of data for myself, and then op-pa … and there is no data. But the list is:

The most amazing thing is that I forgot to specify the value from the list 🙂 I fill in the gap:
del list [0], where 0 is the deleted item in the list 🙂