There is a append () function, but it adds an element to the end of the list, what function to use it to be added to the beginning?
For example
[10,100,55]
Add 5 to the beginning
[5,10,100,55]
Answer 1, Authority 100%
l = [10,100,55]
Option 1:
l.insert (0, 5)
Option 2:
l = [5] + l
Answer 2, Authority 75%
You can use the insert
method, which inserts the item in the position specified by the first argument.
Example:
def main ():
Li = [10, 100, 55]
Li.insert (0, 5)
Print (LI)
if __name__ == '__main__':
Main ()
stdout:
[5, 10, 100, 55]
Answer 3
l.insert (0,5)
l.insert – function with which we add to the very beginning of the list
(0), and then we indicate what we want to add
In our case – 5.
That is: L.Insert – add,
0 – the place to which you want to add an item,
and 5 – what you need to add.