append() and extend() in Python
Append: Adds its argument as a single element to the end of a list. The length of the list increases by one.
syntax: # Adds an object (a number, a string or a # another list) at the end of my_list my_list.append(object)
['geeks', 'for', 'geeks']
NOTE: A list is an object. If you append another list onto a list, the parameter list will be a single object at the end of the list.
['geeks', 'for', 'geeks', [6, 0, 4, 1]]
extend(): Iterates over its argument and adding each element to the list and extending the list. The length of the list increases by number of elements in it’s argument.
syntax: # Each element of an iterable gets appended # to my_list my_list.extend(iterable)
['geeks', 'for', 6, 0, 4, 1]
NOTE: A string is an iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string.
['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']
Append has constant time complexity i.e.,O(1).
Extend has time complexity of O(k). Where k is the length of list which need to be added.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course