Python is a dynamically used high-level programming language applied widely for various tasks such as web development, data analysis, machine learning, and more. One of the crucial features of Python is its ability to work with functions, essentially blocks of code that can be called and executed when needed.
Even though functions are an essential part of Python, some confusing aspects can trick beginners and even experienced developers. This blog post will investigate the perplexing Python functions and suggest avoiding frequently encountered problems.
Append and Iterable
Before jumping into the functions, let's understand append and iterable.
In computer programming, append typically refers to adding an element or item to the end of a list, array, or other data structure. This is often done using a method or function called append()
. The new element becomes the last item in the sequence, with all existing elements remaining in their original positions.
Whereas an iterable in computer programming refers to any object or data structure that can be looped over or iterated sequentially. This includes lists, tuples, dictionaries, sets, strings, and other collection types that contain multiple items or elements. Iterating over an iterable allows you to access each element individually and perform a few actions. Actions such as printing it to the screen or performing a calculation. This is typically done using a loop construct such as a for
loop or a while
loop.
The Tricky Functions
append()
and extend()
In Python, append()
and extend()
are two list methods that are used to add elements to a list, but they differ in their functionality.
append()
To add a single element to the end of a list, append()
is used. It takes a single argument, which can be any data type, and appends it to the end of the list.
extend()
On the other hand, extend()
is used to add multiple elements to a list. It takes an iterable (a list, tuple, or string) as an argument and adds each element of the iterable to the end of the list.
extend()
like you did in append()
, it will result in a TypeError: 'int' object is not iterable
because only one integer is not iterable.So, append()
is used to add a single element to the end of a list, while extend()
is used to add multiple elements (from an iterable) to the end of a list.
remove()
, pop()
, and clear()
In Python, remove()
, pop()
, and clear()
are three list methods that are used to remove elements from a list, but they differ in their functionality.
remove()
The remove()
function is used to remove the first occurrence of a specific value from the list. It takes a single argument, which is the value to be removed and removes the first occurrence of that value from the list.
pop()
The pop()
function removes an element from the list based on its index. It takes an optional argument, the element's index, to be removed. If no argument is provided, pop()
removes and returns the last element of the list.
clear()
The clear()
function removes all the elements from the list, making it an empty list. It doesn't take any arguments.
To put it briefly, remove()
eliminates the initial occurrence of a specific value from the list, pop()
deletes an element from the list depending on its index, and clear()
eliminates all the elements from the list.
sort()
vs sorted()
In Python, both sort()
and sorted()
are used for sorting elements in a list. However, they differ in terms of their functionality and usage.
sort()
The sort()
method is an in-place sorting algorithm that modifies the original list. It returns nothing, and the list is sorted in ascending order by default. You can also specify a reverse order by passing the reverse=True
parameter to sort()
.
sorted()
On the other hand, sorted()
is a built-in function that creates a new sorted list based on the elements of an iterable. It returns a new sorted list and doesn't modify the original list.
One notable difference between the two is that sorted()
can sort any iterable, whereas sort()
is only available for lists.
In summary, sort()
is used for sorting a list in place, while sorted()
is used for creating a new sorted list without modifying the original one.
zip()
vs zip_longest()
zip()
and zip_longest()
are two built-in Python functions that can be used to combine multiple iterables into a single iterable.
zip()
The zip()
function takes one or more iterables as arguments and returns an iterator that aggregates elements from each iterable. It stops when the shortest iterable is exhausted. For example:
In this example, the zip()
function returns an iterator that combines elements from list1
and list2
until the end of the shortest iterable is reached.
zip_longest()
The zip_longest()
function is similar to zip()
, but it continues iterating until the longest iterable is exhausted, filling in missing values with a specified fill value (which defaults to None
). For example:
In this example, since list2
is shorter than list1
, the missing value is filled with missing
to create a new iterable that is the same length as list1
.
In summary, zip()
and zip_longest()
can be used to combine multiple iterables into a single iterable, but zip_longest()
will fill in missing values when the iterables are not of equal length.
Lastly,
there are still many other confusing functions, but these are some of the most common ones I have encountered while working with Python. Hope I have described the functions in the easiest way possible. Let me know if you have any other confusing functions in mind or if you also found these functions tricky when you first started applying them.
Thanks for reading. Subscribe for more!