[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

Lists

The document provides an overview of lists in Python, describing their characteristics as ordered, mutable sequences that can contain mixed data types. It covers accessing elements, various list operations such as concatenation, repetition, and membership, as well as methods for traversing and manipulating lists. Additionally, it discusses nested lists, copying methods, and the behavior of lists when passed as function arguments.

Uploaded by

Neeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Lists

The document provides an overview of lists in Python, describing their characteristics as ordered, mutable sequences that can contain mixed data types. It covers accessing elements, various list operations such as concatenation, repetition, and membership, as well as methods for traversing and manipulating lists. Additionally, it discusses nested lists, copying methods, and the behavior of lists when passed as function arguments.

Uploaded by

Neeta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C2TI 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.

(A) List Traversal Using for Loop:

>>> list1 = ['Red','Green','Blue','Yellow', 'Black']

>>> for item in list1: print(item)

Output:

Red

Green

Blue

Yellow

Black

• Another way of accessing the elements of the list is using range() and len()
functions:

>>> for i in range(len(list1)):

print(list1[i])

Output:

Red

Green

Blue

Yellow

Black

(B) List Traversal Using while Loop:

>>> list1 = ['Red','Green','Blue','Yellow', 'Black']

>>> i = 0

>>> while i < len(list1):

print(list1[i])

i += 1

(output is given in next page)

2
C2TI LISTS

Output:

Red

Green

Blue

Yellow

Black

➢ List Methods and Built-in Functions:


• The data type list has several built-in methods that are useful in
programming.
Method Description Example
len() Returns the length of the list >>> list1 = [10,20,30,40,50]
passed as the argument. >>> len(list1)
5
list() Creates an empty list if no >>> list1 = list()
argument is passed. >>> list1
[]
Creates a list if a sequence is >>> str1 = 'aeiou'
passed as an argument. >>> list1 = list(str1)
>>> list1
['a', 'e', 'i', 'o', 'u']
append() Appends a single element >>> list1 = [10,20,30,40]
passed as an argument at the >>> list1.append([50,60])
end of the list.The single element >>> list1
can also be a list. [10, 20, 30, 40, [50, 60]]
extend() Appends each element of the list >>> list1 = [10,20,30]
passed as argument to the end >>> list2 = [40,50]
of the given list >>> list1.extend(list2)
>>> list1
[10, 20, 30, 40, 50]
insert() Inserts an element at a particular >>> list1 = [10,20,30,40,50]
index in the list >>> list1.insert(2,25)
>>> list1
[10, 20, 25, 30, 40, 50]
count() Returns the number of times a >>> list1 = [10,20,30,10,40,10]
given element appears in the list >>> list1.count(10)
3
index() Returns index of the first >>> list1 = [10,20,30,20,40,10]
occurrence of the element in the >>> list1.index(20)
list. If the element is not present, 1
ValueError is generated
remove() Removes the given element from >>> list1 = [10,20,30,40,50,30]
the list. If the element is present >>> list1.remove (30)
3
C2TI LISTS

multiple times, only the first >>> list1


occurrence is removed. If the [10, 20, 40, 50, 30]
element is not present, then
ValueError is generated
pop() Returns the element whose >>> list1 = [10,20,30,40,50,60]
index is passed as parameter to >>> list1.pop (3)
this function and also removes it 40
from the list. If no parameter is >>> list1
given, then it returns and [10, 20, 30, 50, 60]
removes the last element of the
list
reverse() Reverses the order of elements >>> list1 = [34,66,12,89,28,99]
in the given list >>> list1.reverse()
>>> list1
[ 99, 28, 89, 12, 66, 34]
sort() Sorts the elements of the given >>> list1 = [34,66,12,89,28,99]
list in-place >>> list1.sort(reverse = True)
>>> list1
[99,89,66,34,28,12]
sorted() It takes a list as parameter and >>> list1 = [23,45,11,67,85,56]
creates a new list consisting of >>> list2 = sorted(list1)
the same elements arranged in >>> list1
sorted order [23, 45, 11, 67, 85, 56]
>>> list1 = [34,12,63,39,92,44]
min() Returns minimum or smallest >>> min(list1)
element of the list 12

max() Returns maximum or largest >>> max(list1)


element of the list 92

sum() Returns sum of the elements of >>> sum(list1)


the list 284

➢ 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[:]

Method 2: We can use the built-in function list() as follows:

newList = list(oldList)

Method 3: We can use the copy () function as follows:

import copy #import the library copy

#use copy() function of library copy

newList = copy.copy(oldList)

➢ List as Argument to a Function:


• Whenever a list is passed as an argument to a function, we have to consider
two scenarios:

(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)]

ALL THE BEST

You might also like