3 Important “vs.” Among Python Functions and Methods
Similar yet very different tools, explained
Python’s built-in functions and methods make it a highly versatile programming language that allows for short development times.
Amongst the rich selection of functions and methods, there are some similar ones in terms of what they do. But, of course, they’re not the same. What makes them different might have serious consequences if not used properly.
In this article, we’ll learn 3 pairs of similar yet very different Python functions.
1. Append vs Extend
Both append
and extend
can be used for adding new items to a Python list.
append
adds a single item to a listextend
adds the items in the given collection to a list
Here is an example:
# create a list
mylist = ["John", "Jane", "Emily"]
# add an item
mylist.append("Max")
# check the list
print(mylist)
['John', 'Jane', 'Emily', 'Max']
The append
method added the string “Max” to mylist
as a new item. Let’s see what happens if we do the same operation using the extend
method.
# extend the list with Max
mylist.extend("Max")
# check the list
print(mylist)
['John'…