17 Quick Examples to Learn Python Lists | by Soner Yıldırım | Towards Data Science

Data structures are the key components of programs. They hold or contain the data in a particular way to make the programs work more efficiently. How to access the data stored in a data structure is of crucial importance as well.
Lists are one of the most commonly used built-in data structures of Python. Lists are capable of storing values with different data types which makes them highly useful.
There are various methods and functions that can be applied to lists. We should master them in order to make the most out of lists in our programs. The way you handle data is key to create efficient programs.
In this article, we will go over 17 quick examples to demonstrate how to interact with and manipulate Python lists. Let’s first create a simple list that contains some integers and strings.
mylist = [1, 2, 3, 4, 4, 4, 5, 5, 'a', 'b']
The count method returns the number of occurrences of an item in a list.
mylist.count(4)3mylist.count('a')1
The pop method removes an item from a list. By default, it removes the last item but we can specify the index of the item to be removed.
mylist = [1, 2, 3, 4, 4, 4, 5, 5, 'a', 'b']mylist.pop()'b'print(mylist)[1, 2, 3, 4, 4, 4, 5, 5, 'a']
Let’s use it to remove the second item.
mylist.pop(1)2print(mylist)[1, 3, 4, 4, 4, 5, 5, 'a']
Another option to remove an item from a list is the remove method. It requires to specify the item to be removed rather than its index.
mylist = ['a', 'b', 'a', 'c', 'd', 'a']mylist.remove('a')print(mylist)['b', 'a', 'c', 'd', 'a']
If there are multiple items with the same value, the remove method removes the first occurrence.
There is an important difference between the pop and remove methods. The pop returns the removed item so it can be assigned to a new variable. On the other hand, the remove method does not return anything.
mylist = ['a', 'b', 'c', 'd']var_pop = mylist.pop()var_remove = mylist.remove('a')print(var_pop)dprint(var_remove)None
The clear method removes all the items in a list so we end up having an empty list.
mylist = ['a', 'b', 'c', 'd']mylist.clear()print(mylist)[]
We can add a new item to a list by using the append method.
mylist = ['a', 'b', 'c', 'd']mylist.append(1)print(mylist)['a', 'b', 'c', 'd', 1]
If you try to append a list to another list, the appended list will be an item in the original list.
mylist = ['a', 'b', 'c', 'd']myotherlist = [1, 2, 4]mylist.append(myotherlist)print(mylist)['a', 'b', 'c', 'd', [1, 2, 4]]
If you want to append each item separately, use the extend method instead.
mylist = ['a', 'b', 'c', 'd']myotherlist = [1, 2, 4]mylist.extend(myotherlist)print(mylist)['a', 'b', 'c', 'd', 1, 2, 4]
We can use the list constructor to create a list based on the characters in a string.
mylist = list("python")print(mylist)['p', 'y', 't', 'h', 'o', 'n']
The reverse method can be used to reverse the order of items in a list. It does the reversing operation in place so nothing is returned.
mylist.reverse()print(mylist)['n', 'o', 'h', 't', 'y', 'p']
We can sort the items in a list with the sort method.
mylist = ['g','a', 'c', 'd']myotherlist = [4, 2, 11, 7]mylist.sort()myotherlist.sort()print(mylist)['a', 'c', 'd', 'g']print(myotherlist)[2, 4, 7, 11]
The sort method sorts in ascending order by default. It can be changed by using the reverse parameter.
mylist = ['g','a', 'c', 'd']myotherlist = [4, 2, 11, 7]mylist.sort(reverse=True)myotherlist.sort(reverse=True)print(mylist)['g', 'd', 'c', 'a']print(myotherlist)[11, 7, 4, 2]
The sort method works in place. Thus, it modifies the original list but does not return anything. If you want to update the original one but need a sorted version of it, the built-in sorted function can be used.
mylist = ['g','a', 'c', 'd']mysortedlist = sorted(mylist)print(mylist)['g', 'a', 'c', 'd']print(mysortedlist)['a', 'c', 'd', 'g']
The original one remains the same. We have a new list which is the sorted version of the original one.
We have seen the append method which is used to add a new item to a list. It adds the new item at the end.
The insert method offers more flexibility. It allows for specifying the index for the new item.
mylist = [1, 2, 3, 4, 5]mylist.insert(2, 'AAA')print(mylist)[1, 2, 'AAA', 3, 4, 5]
The first parameter is the index and the second one is the item to be added.
The built-in len function returns the number of items in a list.
mylist = [1, 2, 3, 4, 5]len(mylist)5
We can use the “+” operator to combine multiple lists.
first = [1, 2, 3, 4, 5]second = [10, 11, 12]third = [1, 'A']first + second + third[1, 2, 3, 4, 5, 10, 11, 12, 1, 'A']
We can obtain a list by splitting a string. The split function creates a list that contains each part after splitting.
text = "Python is awesome"mylist = text.split(" ")print(mylist)['Python', 'is', 'awesome']
We specify the character to be used as the split point which is space in our case.
List comprehension is a technique to create lists based on other iterables. For instance, we can create a list based on another list.
mylist = ["This", "is", "a", "great", "example"]newlist = [len(x) for x in mylist]print(newlist)[4, 2, 1, 5, 7]
The new list contains the lengths of the items in the original list.
List comprehension is a highly important topic. If you’d like to learn more, here is an article that focuses on list comprehension.
towardsdatascience.com
Lists are one of the core data structures of Python. They are usually used as building blocks in programs. Thus, it is very important to get comfortable working with lists.
The examples we have done pretty much cover all you need with Python lists. The rest is just to practice.
Thank you for reading. Please let me know if you have any feedback.