Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
51 views

Lists in Python

The document discusses lists in Python including list operations, slicing, built-in functions, and methods. Lists can contain different data types and support operations like concatenation, membership testing, indexing, slicing, and common functions like len, max, min. Methods like append, insert, remove, sort are also covered with examples.

Uploaded by

Chinmay Khanna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Lists in Python

The document discusses lists in Python including list operations, slicing, built-in functions, and methods. Lists can contain different data types and support operations like concatenation, membership testing, indexing, slicing, and common functions like len, max, min. Methods like append, insert, remove, sort are also covered with examples.

Uploaded by

Chinmay Khanna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LIST

 A list is a sequence of comma separated values (items) enclosed in square brackets.


 The items in a list need not have the same type.
For example, consider this list with multiple values defined using commas:
L1 = [ ‘English’, 45, ‘Maths’, 86, ‘Rajesh’, 1234]
L2 = [12, 15, ‘Manish’, [45, 87], 23]
L3 = [‘sonia’, (‘E001’, ‘D001’), 34000]
You can even have an empty list with two square brackets and no content.
L1 = [ ]

Basic List operations

Expression Results Description


[1, 5, 6] + [7, 8, 10] [1,5,6,7,8,10] Concatenation – joins the lists into a
single list.
[‘Welcome’] * 2 [‘Welcome’, Repetition – repeats the list
‘Welcome’]
4 in [3, 4, 5] True Membership – returns True if the
2 in [3, 4, 5] False element is a member of list otherwise
2 not in [3, 4, 5] True False.

List Slicing
• Just like strings, values in a list can be accessed by using their index or indices.
• You can update a single or multiple element in a list by giving the index on the left
side of the assignment operator.
• To remove a particular list element, you can use the del statement.

Slicing List
Slicing operation Explanation
L1[3] It accesses the fourth element in the list.
L1[2:5] It accesses elements starting from the second index to fourth index.
L1[1:-2] It accesses elements starting from the first index upto the -3 index.
L1[ :4] It omits the first index to start accessing elements from the beginning
upto the third index.
L1[2: ] It omits the last index to start accessing elements from the second
index to the end.
L1[1] = 90 It updates the element present at index 1.
del L1[3] It removes the element present at index 3.

Fig. 6.6 shows some examples of list slicing in Python window.

List slicing

Built-in List functions

Consider the list L1 = [ 4, 7, 10, 11, 17, 20]

Function Description
len(L1) It returns the number of elements in the list.
max(L1) It returns the item from the list with maximum value.
min(L1) It returns the item from the list with minimum value.
sum(L1) It returns the sum of all the elements in the list.
tuple(L1) It converts the list into a tuple.

List Methods
Python includes the following list methods:
list.append(obj) The method appends object obj to the end of the list.
list.count(obj) It returns a count of how many times an object obj occurs in the list.
list.extend(seq) It appends the content of seq to the list..

list.index(obj) It returns the lowest index in the list where obj appears.
list.insert(index,obj) This function inserts the object obj into the list at the particular index.

list.pop( ) This method is used to remove and return the last object from the list .
list.remove(obj) It removes the first instance of the object obj from the list .

list.reverse( ) It reverses objects of a list.


list.sort( ) It sorts the objects of list in ascending order. If the list has both numeric and
nonnumeric values then first numeric and then the non-numeric values will be displayed.
The figure 6.8 shows the list methods.
Fig. 6.8 List methods

Practice Time
Write a Python script to create a list of numbers and display the count of numbers,
maximum, minimum, sum and average of numbers in the list.
a. Consider a list with name Test, contains 10 elements. You can access 4th element from the
test using
i. test[4] ii. Test[5] iii. Test[3] iv. Test[2]

b. What is the output of the following code?


A = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
print (A[ : -4]
i. [ab] ii. [‘a’, ‘b’] iii. [‘d’, ‘e’] iv. Error
e. What will be the output, if you run the following code?
A = [[1,2,3], [4,5,6], [7,8,9]]
print (A[2])
i. [4,5,6]
ii. [1,2,3]
iii. [7,8,9]
iv. 2

2. Consider the list L1 = [12, 15, [16, ‘Bhumika’], 85]


What will be the output of print (L[2])?

You might also like