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.
python append list
syntax:
Adds an object (a number, a string or a
another list) at the end of my_list
mylist.append(object) filternone
edit
play_arrow
brightness4 mylist = ['geeks', 'for']
mylist.append('geeks') print mylist
Output:
['geeks', 'for', 'geeks']
NOTE: A list is an object project. If you append another list onto a list, the parameter list will be a single object at the end of the list.
filternone edit playarrow
brightness4 mylist = ['geeks', 'for', 'geeks']
anotherlist = [6, 0, 4, 1] mylist.append(anotherlist) print mylist
Output:
['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:
python tic tac game project code
Each element of an iterable gets appended
to my_list
mylist.extend(iterable) filternone
edit
play_arrow
brightness4 mylist = ['geeks', 'for']
anotherlist = [6, 0, 4, 1] mylist.extend(anotherlist) print mylist
Output:
want to compile program in smart way
['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.
filternone edit playarrow
brightness4 mylist = ['geeks', 'for', 6, 0, 4, 1]
mylist.extend('geeks') print mylist
Output:
['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']
Time Complexity:
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.
Reference: https://www.pythonslearning.com/
This site is built on Heroku
Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)