FREE INTERACTIVE ONLINE COURSE

Python 3

FOR BEGINNERS


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)


ALL REVIEWS
LESSON 10
PAGE 5 / 5
Lists
Home >>> Free Online Lessons, Python 3

Adding Elements

We have two functions available for adding new elements to a list, and these are:

1. The append(value) method adds a new element at the end of the list, with the value sent as an argument:



Obviously, the length of the list increases by one each time.

2. The insert(index, value) method is superior to the previous one because it adds a new element at the desired position in the list, shifting all existing elements to the right by one:



Thus, we can insert a new element at the desired position in the list.

EXAMPLE
Editor - lesson_10.py
       
Console/Output done
Interesting, right?

More would mean a chapter of a book. Python is object-oriented, and the values we refer to that can be introduced as list elements are actually objects. So, an element of a list can be ... another list! Look at this small example:

The lesson is now over.
 home   list  CONTENTS   perm_identity   arrow_upward