Lists
Lists
LISTS
➢ Introduction to List:
• The data type list is an ordered sequence which is mutable and made up of
one or more elements.
• Unlike a string which consists of only characters, a list can have elements of
different data types, such as integer, float, string, tuple or even another list.
• A list is very useful to group together elements of mixed data types.
• Elements of a list are enclosed in square brackets and are separated by
comma.
• Like string indices, list indices also start from 0.
➢ Accessing Elements in a List:
• The elements of a list are accessed in the same way as characters are
accessed in a string.
➢ Lists are Mutable:
• In Python, lists are mutable. It means that the contents of the list can be
changed after it has been created.
➢ List Operations:
• The data type list allows manipulation of its contents through various
operations as shown below.
❖ Concatenation:
▪ Python allows us to join two or more lists using concatenation
operator depicted by the symbol +.
▪ If we want to merge two lists, then we should use an assignment
statement to assign the merged list to another list.
▪ The concatenation operator '+’ requires that the operands should be
of list type only.
▪ If we try to concatenate a list with elements of some other data type,
TypeError occurs.
❖ Repetition:
▪ Python allows us to replicate a list using repetition operator depicted
by symbol *.
❖ Membership:
▪ Like strings, the membership operators in checks if the element is
present in the list and returns True, else returns False.
▪ The not in operator returns True if the element is not present in the list,
else it returns False.
❖ Slicing:
▪ Like strings, the slicing operation can also be applied to lists.
1
C2TI LISTS
➢ Traversing a List:
• We can access each element of the list or traverse a list using a for loop or a
while loop.
Output:
Red
Green
Blue
Yellow
Black
• Another way of accessing the elements of the list is using range() and len()
functions:
print(list1[i])
Output:
Red
Green
Blue
Yellow
Black
>>> i = 0
print(list1[i])
i += 1
2
C2TI LISTS
Output:
Red
Green
Blue
Yellow
Black
➢ Nested Lists:
• When a list appears as an element of another list, it is called a nested list.
• To access the element of the nested list of list1, we have to specify two
indices list1[i][j].
• The first index i will take us to the desired nested list and second index j will
take us to the desired element in that nested list.
➢ Copying Lists:
• Given a list, the simplest way to make a copy of the list is to assign it to
another list.
4
C2TI LISTS
• We can also create a copy or clone of the list as a distinct object by three
methods.
• The first method uses slicing, the second method uses built-in function list()
and the third method uses copy() function of python library copy.
Method 1:We can slice our original list and store it into a new variable as follows:
newList = oldList[:]
newList = list(oldList)
newList = copy.copy(oldList)
(A) Elements of the original list may be changed, i.e. changes made to the list in
the function are reflected back in the calling function.
(B) If the list is assigned a new value inside the function then a new list object is
created and it becomes the local copy of the function. Any changes made inside
the local copy of the function are not reflected back to the calling function.
➢ List Manipulation:
• In this chapter, we have learnt to create a list and the different ways to
manipulate lists.
[refer to the given table above (pg.no 3 and 4)]