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.

myList = [1, 2, 3, 4]
myList.append(7)
print(myList) 
Example of append()
[1, 2, 3, 7]
Output

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.

myList = [1, 2, 3, 6, 7]
myList.extend([4, 5, 9])
print(myList)
Example of extend()
[1, 2, 3, 6, 7, 4, 5, 9]
Output
✍️
If you pass a single element to 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.

myList = [1, 2, 3, 6, 7, 6, 7]
myList.remove(6)
print(myList)
Example of remove()
[1, 2, 3, 7, 6, 7]
Output

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.

✍️
I have briefly explained the index in my Slice Function in Python blog. Feel free to refer if you encounter any confusion in the process.
myList = [1, 2, 3, 4, 5]
myList.pop(2)
print(myList)

myList.pop()
print(myList)
Example of pop()
[1, 2, 4, 5]
[1, 2, 4]
Output

clear()

The clear()function removes all the elements from the list, making it an empty list. It doesn't take any arguments.

myList = [1, 2, 3, 4, 5]
myList.clear()
print(myList)
Example of clear()
[]
Output

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().

myList = [3, 2, 1, 5, 4, 6]
myList.sort()
print(myList)
Example of sort()
[1, 2, 3, 4, 5, 6]
Output

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.

myList = [3, 2, 1, 5, 4]
newList = sorted(myList)
print(newList)
print(myList)
Example of sorted()
[1, 2, 3, 4, 5]
[3, 2, 1, 5, 4]
Output

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:

list1 = [1, 2, 3, 4, 5, 6]
list2 = ['a', 'b', 'c']
zipped = zip(list1, list2)
print(list(zipped))
Example of zip()
[(1, 'a'), (2, 'b'), (3, 'c')]
Output

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:

from itertools import zip_longest

list1 = [1, 2, 3, 4]
list2 = ['a', 'b']
zipped_longest = zip_longest(list1, list2, fillvalue='missing')
print(list(zipped_longest))
Example of zip_longest()
[(1, 'a'), (2, 'b'), (3, 'missing'), (4, 'missing')]
Output

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!