Logo

Lists

Python is familiar with a number of compound data types, which are used to group together other values.

The list is the most versatile, and it can be written as a list of comma-separated values (items) between square brackets.

 

 List

Figure 7.1. Lists are used to keep multiple items in one variable.

 

Lists may contain items of various types, but they almost always contain items of the same type.

 

 

Lists, like strings (and all of the other built-in sequence types), can be indexed and sliced.


 

All slice activities return a new list containing the elements that were requested. This means that the slice that follows returns a shallow copy of the list.

 

 

Lists can also perform operations such as concatenation.


 

Unlike strings, which are immutable, lists are mutable, which means their contents can be changed.

 

 

You can also append new items to the end of the list using the append() method (more on methods later):

 

 

It is also possible to assign to slices, which can change the size of the list or clear it entirely.

 

 

Lists are also supported by the built-in function len().

 

 

It is possible, for example, to nest lists (create lists that contain other lists).

 

 

 


________________________________________________________________________________________________________________________________
Footer
________________________________________________________________________________________________________________________________

Copyright © 2022-2023. Anoop Johny. All Rights Reserved.