Python Basic – How To Use List in Python

Home /

Table of Contents

List Operations in Python

In this section, we will discuss some of the lists of the list. In the previous section, we saw how to access an element on the specified index of a list. So let’s see how to add a new element to a particular index or position,

Example:

   numbers = [10, 12, 13, 15]
   numbers[3] = 4
   print(numbers)

Output:

[10, 12, 13, 4]

That is, In the numbers list before 3 positions value was 15 and in that position, we set the new value 4. numbers [3] = 4 thus. And so the printing numbers list came output with the updated value of this list.

Add or Multiply the List

The funny thing is that you can add or multiply the list like a string. For example – see the example below,

Example:

   numbers_list = [1, 2, 3]
   print(numbers_list + [4, 5, 6])
   print(numbers_list * 4)

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

Element Check-in List

The in-operator is used to check if there are any specific elements in a list. If the element is in the list one or more times then it returns True otherwise False returns.

Example:

   skills = ["Python", "C++", "Javascript", "Matlab"]
 
   print("Python" in skills)
   print("Java" in skills)
   print("Javascript" in skills)

Output:

   True
   False
   True

In the same way, the absence of any element can also be checked using the not operator.

Example:

   skills = ["Python", "C++", "Javascript", "Matlab"]
 
   print(not "Python" in skills)
   print("Java" not in skills)
   print("Javascript" in skills)

Output:

   False
   True
   True

List Function

In this section, we will look at some of its built-in methods and some of the functions used to deal with lists. How do we know what methods are available for list manipulation? Here is a small tip for that: in the terminal, in IDLE, or where the Python code is running, you can type dir(list) and enter run/print.

You will get the following output –

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__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']
>>>

A list has been returned. Is that so? Note at the end – … “append”, “count”, “extend”, “index“, “insert”, “pop”, “remove”, ”reverse”, “sort”’ ] i.e. these are built-in methods for list manipulation. That is, you can use it to see a list of all the attributes and methods of the list object. This is not only for lists but also for other methods in python. Do not try to pass other objects in dir().

Append in List

So let’s try append. But you can also learn how it works without asking anyone. One more tip, run help(list.append) and the following output will appear-

>>> help(list.append)
Help on function append:
 
append(self, item)
   L.append(object) --> None --- append object to end
>>>

This means that the function of this method is seen and it is said to append an object to the end. This means that this method can be used to add new elements to the end of a list. If you do not see the example –

Example:

   numbers = [38, 10, 83]
   numbers.append(4)
   print(numbers)

Output:

[38, 10, 83, 4]

Insert in List

The insert method is almost identical but can be used for a slightly different reason. As follows –

Example:

   words_list = ["Awesome", "Geek"]
   index = 1
   words_list.insert(index, "Enable")
 
   print(words_list)

Output:

  ['Awesome', 'Enable', 'Geek']

That is, if you want to add an element to a specific position or index in a list, append will not work (because it adds at the end) but you have to use insert. The two parameters of the insert method – the first is to add a new element to a position in the list and the second parameter is the element you want to add yourself. In the example above, we have added “Enable” to the second position in the list of words i.e. index = 1.

Index of List Elements

Let’s look at the use of another method. E.g. – index. In the following example, we have used the index method to check which of the elements in an index list is in which index.

Example:

   alphabet = ['e', 'n', 'a', 'b', 'l', 'e']
   print(alphabet.index('n'))
   print(alphabet.index('b'))
   print(alphabet.index('l'))
   print(alphabet.index('g'))

Output:

   1
   3
   4
   ValueError: 'g' is not in list

Count of List Elements

The following count() method can be used to calculate the total number of times an element in a list.

Example:

   alphabet = ['e', 'n', 'a', 'b', 'l', 'e']
   print(alphabet.count('e'))
   print(alphabet.count('n'))
   print(alphabet.count('a'))
   print(alphabet.count('b'))
   print(alphabet.count('l'))
   print(alphabet.count('g'))

Output:

   2
   1
   1
   1
   1
   0

Here each element is once but e is twice. Again g is not in this list so it has printed zero.

To know the function of all such methods, you can run help(list.METHOD_NAME) in this way and see the details of that method from the output screen.

Apart from the object method, there are some useful functions for the list. Such as – max(), min(), len() etc. For example, the max() function can be used to see the largest of the elements in a list.

Example:

   numbers = [38, 20, 40, 20, 50, 30, 4]
   print("Maximum: ",max(numbers))
   print("Minimum: ",min(numbers))
   print("Length: ",len(numbers))

Output:

   Maximum:  50
   Minimum:  4
   Length:  7
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article