DEV Community

pagarsach14
pagarsach14

Posted on

1

How python append list

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/

Heroku

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!

Get Started

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay