Everything to know about lists in Python | Nouman | Towards Data Science

Lists in Python, or arrays in other programming languages, are basically a compound data structure that can hold basically a list of items. This article serves as a beginner-friendly way to start with lists in Python. We are going to cover a lot about lists in this article so let’s get started!!
Here’s a list of topics we will discuss on lists (pun intended xD):
Intro to Lists
Common Methods on Lists
Lists Iteration
Using Lists as Stacks
Using Lists as Queues
Conclusion
This is part of my 30-Day Article Writing Challenge. Feel free to check out the articles on my pledge post:
nouman10.medium.com
Python has a lot of data structures that can be used to store a list of items but the list is one of the most versatile ones. It is written as a comma-separated list of values wrapped between square brackets.
array = [1, 2, 3, 4, 5, 6, 7]array
Lists in python can be indexed (0-indexed, negative indexing is allowed) and sliced like follows:
array = [1, 2, 3, 4, 5, 6, 7]print(array[0])print(array[-1])print(array[2:5])
Lists also allow concatenation like follows (but doesn’t modify the original list like this unless you store the value in that variable):
array = [1, 2, 3, 4, 5, 6, 7]print(array + [8, 9, 10, 11])
Lists are mutable i.e, it is possible to change their content like follows:
array = [1, 2, 4, 4, 5, 6, 7] # 4 instead of 3 on index 2array[2] = 3print(array)
We can also assign values to slices of lists too (this may or may not change their length as well):
array = [1, 2, 3, 4, 5, 6, 7]array[2:5] = [5, 4, 3]print(array)array[2:5] = []print(array)
Lists can contain different types of objects as well and even lists within lists! (nested lists)
array_1 = [1, 2, 3, 'a', 'b', 'c']array_2 = [4, 5, 6, 7]combined = [array_1, array_2]print(combined)
That’s all for an intro to lists. Let’s look at some of the common methods used on lists
Here we will look into some of the common methods applied on lists in python and at the end, we will see a demo for all these together:
Note: list , list_1 , list_2 all represent some kind of arbitrary lists.
As the name suggests, it returns the length of a list.
Syntax: len(list)
It allows us to add any item to the end of the list.
Syntax: list.append(item)
It appends all the items from one list to the other.
Syntax: list_1.extend(list_2)
It inserts the given item on the mentioned index
Syntax: list.insert(index, item)
It removes the first occurrence of the given item from the list. In case the item is not present, it raises a ValueError
Syntax: list.remove(item)
It removes the item from the given index and returns that item. In case of no given index, it removes the last item from the list and returns that.
Syntax: list.pop() or list.pop(index)
As the name suggests, it clears the list (empties it)
Syntax: list.clear()
It searches the list for a given item and returns the index of its first occurrence in the list and -1 if it is not present in the list. It also has optional parameters, start, and end, which limit the search space inside the list
Syntax: list.index(item)
Returns the number of times the given item appears in the list
Syntax: list.count(item)
Sorts the list in place i.e modifies the original list
Syntax: list.sort()
Reverses the list in place
Syntax: list.reverse()
Returns a shallow copy of the list
Syntax: list.copy()
List iteration is a topic on its own which I have covered in length in a previous article of mine:
towardsdatascience.com
Stacks perform operations on the basis of the LIFO (Last In, first-out) algorithm. Lists can act as stacks quite easily by using their built-in methods. To append any element to the end of the list, we can use list.append(item) and to retrieve any element from that list, we can use list.pop() without any index, which will remove and return the last (appended) element in the list.
Queues perform operations on the basis of the FIFO (First In, first-out) algorithm. We can use lists for this purpose by using their methods, pop and insert with indices but that’s not efficient because while the append and pop from the end of lists are quite fast, pop and insert at the start of the list is not that efficient as all the elements have to be shifted by one index.
Luckily, Python has a built-in collections.deque that implements fast appends and pops from both ends.
To conclude:
We started with a basic introduction to lists in Python
Then we looked into some built-in methods for lists
Finally, we looked into how lists can be used as stacks and queues.
If you feel that above content was useful for you, do share it and feel free to support me ->