By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
We have already discussed about Lists as Python data type. In this chapter we are going to discuss basic lists operations and Python in-built methods and functions for Lists.
Creating a list A list in Python can be declared by putting different comma-separated values between square brackets. e.g. days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; number = [1, 2, 3, 4, 5,6,7,8,9,0 ]; chars = ["a", "b", "c", "d", "e"];
Accessing elements from list Elements from the list can be accessed by using the square brackets for slicing along with the index to procure value available at that index of the list. E.g. days [1]; number [1:6];
Updating and Adding elements in lists We can update a single element or multiple elements of a list by giving the slice on the left-hand side of the assignment operator. E.g. days [2] = 'January'; We can add elements in the list by using the append () method
Deleting an element from list In Python, we can remove a list element by using the del statement if index is known of the element to be deleted. E.g. del days [3];
Alternatively, we can use the remove () method to remove an element from a list. All of these list operations are demonstrated in the below example. First, we created the list. Next, we accessed the elements from the list. Then, we updated 3rd element in the list using the assignment operator. Lastly, we used the del statement to delete an element in the list, doing so days [2] has printed Wednesday as element which was present at days [2] index was deleted.
Basic lists operations Like Python strings, we can use + and * operators on lists for operations like concatenation and repetition respectively. Below are the list operations on Python lists.
Python Expression
Results
Description
len([1, 2, 3, 4, 5])
5
Length operation.
[1, 2, 3, 4, 5] + [6, 7, 8, 9, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Concatenation operation.
['Hello'] * 3
['Hello', 'Hello', 'Hello']
Repetition operation.
4 in [1, 2, 3, 4, 5]
TRUE
Membership operation.
for a in [1, 2, 3, 4, 5]: print a,
1 2 3 4 5
Iteration operation.
Indexing, Slicing, and Matrixes in Lists Python lists are nothing both sequences, therefore indexing and slicing work the same way for lists as they do for strings.
Therefore, below operations are possible. In the above example, if index is negative then counting will start from the right side and if it starts with 0 then counting will be from left side.
Like Python String, Python lists supports slicing and will print the sections of the lists as per their indexes.
Built-in Lists Methods & Functions Below are Python built-in functions for List operations.
Function
cmp(list1, list2)
This Python built-in function for lists compares elements of both lists.
len(list)
This Python built-in function for lists gives the total length of the list.
max(list)
This Python built-in function for lists returns item from the list with max value.
min(list)
This Python built-in function for lists returns item from the list with min value.
list(seq)
This Python built-in function for lists converts a tuple into list.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.