Python List append method with Example – Python Tutorial

1 year ago Lalit Bhagtani 0

In this tutorial, we will learn about python list append method.
Python List Append

append method :

This method is used to add an element ( item ) to the end of the list.

Syntax : < List Object >.append( item ) 

Example:

[datacamp_exercise lang=”python” height=”350″ min-height=”200″ max-height=”500″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# create list object and assign it to variable l
l = [‘a’,’b’,’c’,’d’,’e’]

# call append() method by passing ‘f’ as an argument
l.append(‘f’)

# print the list, to check its elements
print(l)

# another way of appending element in the list (without using append() method)
l[len(l):len(l)] = ‘g’

# print the list, to check its elements
print(l)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[/datacamp_exercise]

References :-

  1. append method Docs

That’s all for Python List append method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous