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

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay