In the previous post, we had gone through list creationindexing and slicing. Let us explore the power of lists further.

Can we append to lists?

The operation of adding elements at the end of a mutable sequence is known as append. Since lists can be modified (aka mutable), it is perfectly legal to apply this operation on objects of type list. To do this, we have the built-in append() function. We can append integers to a list:

Image for post

Appending 99 to my_list

Not only single-valued constants like integer, float, boolean and string, a list object can also be appended with another list:

Image for post

Appending a list to my_list

A question that immediately arises, “How do we index a list, like the one shown above, which contains another list?“

We treat the newly added list as an item and index it by position, which happens to be 6 in the above scenario:

Image for post

#python3 #programming #lists-in-python #programming-languages #python-programming

Python Lists II and Can we append to lists?
1.55 GEEK