7 List
7 List
List
List
List
List in Python
Creating Lists
Access values in Lists
• To access values in lists, square brackets are used to slice along with
the index or indices to get values stored at that index.
• seq =Name of the List[ start : end : step]
• seq = List[ : : 2]
• Seq = List [ 1: :2]
Accessing the elements of the list using
slice operation
Some List modification operations
Some List modification operations
Some List modification operations
Basic List operations
Basic List operations
List Methods
List methods
Aliasing
Since variables refer to objects, if we assign one variable to another, both variables refer to
the same object. For example:
>>> a = [1, 2, 3]
>>> b=a
In this case, the state diagram looks like this:
Because the same list has two different names, a and b, we say that it is aliased.
Changes made with one alias affect the other. For example:
>>> b[0] = 5
>>> print a
[5, 2, 3]
Cloning lists
If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of
the list itself, not just the reference. This process is sometimes called cloning, to avoid the ambiguity of
the word “copy”.
The easiest way to clone a list is to use the slice operator. For example:
>>> a = [1, 2, 3]
>>> b = a[:]
>>> print b
[1, 2, 3]
Taking any slice of a creates a new list. In this case the slice happens to consist of the whole list.
Nested List
• Nested list means a list within another list
• L = [1,”a”,”abc”,[2,3,4,5],8,9]
Two Dimensional List
• A two-dimensional list is a list that contains other lists as its
elements.
• A two-dimensional list consists of a list of one-dimensional lists, you
can use a list to store two-dimensional data, such as a matrix or a
table, as well.
• For example, the following table, which provides the distances
between cities, can be stored in a list named distances.
Three Dimensional List
Searching an element in List