Python List Methods

Introduction

Using methods with lists gives you the power to manipulate the data stored in the lists quickly and effectively. Many of these methods are concerned with finding, adding, and removing pieces of data stored within the list they are being called on. Others are more concerned with the structure of the list itself.

In any case, they will make your experience using lists a lot less frustrating by saving you time and effort writing and rewriting the same code.

Finding the Length

Yes, this does start off with some more overlap with strings. The len() method works on lists as well. So, in order to find the amount of elements in a list, just place it in the len() method.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
print(len(linux_distros))

Again, keep in mind that the result is the number of elements in the list. The last element is available at the index of six. You can also use the len() method to access elements in the list, if you have to.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
print(linux_distros[len(linux_distros) - 1])

Adding the - 1 at the end is necessary, because there is no index of seven, since the list starts counting at zero. Using this method is another way to access elements based on the length of the list. In some cases, it may be preferable to using the negative numbers.

Append and Pop

What happens when you need to add elements to the list or pop them off of the end? Well, there are two methods, append() and pop(), that make doing so very simple.



Append

In order append() an element onto an array, call the method with the element in the parenthesis.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.append("Mint")
print(linux_distros)

Now, “Mint” is the last element at the end of the list.

Pop

The pop() method can work in two different ways. If you leave the parenthesis empty, it works as the opposite of append().

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.pop()
print(linux_distros)

After running pop(), “Gentoo” is missing off of the end end of the list.

You can also specify what element you want to remove by giving it the index of that element.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.pop(4)
print(linux_distros)

Now, “OpenSUSE” is gone. Using pop() this way is an excellent way to remove elements that you know the index of.

Insert and Remove

There are other ways to insert and remove elements from a list. append() and pop() are concerned mostly with the end of the list. Aside from passing pop() and index, they don’t help much for handling elements anywhere else in the list. That’s why insert() and remove exist. They offer ways to insert and remove elements from anywhere in a list.

Insert

In order to use insert() to place a new element into a list, you have to specify the index where you want to insert your new element and the element that you want to insert.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.insert(2, "Mint")
print(linux_distros)

“Mint” becomes the element at the index two and pushes the other elements back. Take a look at what’s at index four now.

print(linux_distros[4])

“CentOS” is now at four because “Mint” took the place of “Fedora” at two, pushing it to three, where “CentOS” was previously.



Remove

remove() works sort of like giving pop an index to remove, but instead of giving remove() an index you give it the actual value of the element. It will find and remove the first occurrence of that value in the list. So, be sure that the first occurrence is the one that you want to remove. Otherwise, it’s probably best to figure out the index of the element that you do want to remove and use pop().

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.remove("Arch")
print(linux_distros)

“Arch” has been removed from the list, and the index of “Gentoo” has been adjusted accordingly.

Extend

extend() adds two lists together. It adds the list in parenthesis to the list that it is being called on.

linux_distros = ['Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
debian_distros = ['Debian', 'Ubuntu', 'Mint']
linux_distros.extend(debian_distros)
print(linux_distros)

The elements from debian_distros have been added on to the end of linux_distros.

Index

There is a way to find out the index of an element, and that’s the index() method. Like with the others, index() finds the first occurrence of an element, not every occurrence.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
print(linux_distros.index("Arch"))

It just prints out the index number of the element.

Sort

If you need to sort a list, either alphabetically or numerically, Python has you covered there as well. Just call the sort() method on the list, and it will be saved in the new order. This will overwrite the previous list, so create a copy to sort, if you need to preserve the original.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.sort()
print(linux_distros)

The list is now in alphabetical order.

It also works well with numbers. sort() will take a list of numbers and place them in order by value.

numbers_to_sort = [1, 5, 8, 16, 3, 75, 4, 23, 9, 15, 8, 32]
numbers_to_sort.sort()
print(numbers_to_sort)


Reverse

reverse() is a bit of an odd method. It reverses the order of elements in a list in place and saves it. It doesn’t put them in any kind of special order, it just flips the list around.

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.reverse()
print(linux_distros)

If you want to put a list in reverse alphabetical order, just call both sort() and reverse().

linux_distros = ['Debian', 'Ubuntu', 'Fedora', 'CentOS', 'OpenSUSE', 'Arch', 'Gentoo']
linux_distros.sort()
linux_distros.reverse()
print(linux_distros)

Conclusion

By making use of these methods, you can master the manipulation of lists in Python. Again, lists are an integral part of programming, and are something to be practiced at length. The accompanying methods are no exception. Try using them in conjunction with one another to get the most out of their potential. The next guide will explore a new “dimension” of lists.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions


Comments and Discussions
Linux Forum