Lists
Lists — Study Notes
NCERT-aligned · 10 notes · 3 shown free
9.1 INTRODUCTION TO LIST
Concept9.1 INTRODUCTION TO LIST
In Python, a list is a fundamental data type that represents an ordered sequence of elements. Unlike strings, which are sequences of characters, lists can contain elements of different data types such as integers, floats, strings, tuples, or even other lists. This flexibility makes lists very useful for grouping together heterogeneous data. Lists are mutable, meaning their contents can be changed after creation. Elements in a list are enclosed within square brackets [ ] and separated by commas. The indexing of list elements starts at 0, similar to strings, allowing access to elements by their position. For example, a list of even numbers can be written as [2, 4, 6, 8, 10, 12], and a list of vowels as ['a', 'e', 'i', 'o', 'u']. Lists can also be nested, meaning a list can contain other lists as elements, which is useful for representing complex data structures like matrices or tables.
- Lists are ordered sequences of elements enclosed in square brackets.
- Lists can contain elements of mixed data types including integers, floats, strings, tuples, and other lists.
- Indexing in lists starts at 0, similar to strings.
- Lists are mutable, allowing modification after creation.
- Nested lists are lists containing other lists as elements.
- Lists are useful for grouping heterogeneous data.
- 📌 List: An ordered, mutable sequence of elements enclosed in square brackets.
- 📌 Mutable: Ability to change the contents after creation.
- 📌 Nested List: A list that contains other lists as elements.
9.1.1 Accessing Elements in a List
Concept9.1.1 Accessing Elements in a List
Elements in a list are accessed using indices, similar to accessing characters in a string. The first element is at index 0, the second at index 1, and so on. Python also supports negative indexing, where -1 refers to the last element, -2 to the second last, and so forth. Accessing an index outside the range of the list length results in an IndexError. The length of a list can be obtained using the len() function. Expressions can be used inside the index brackets to dynamically access elements. For example, list1[1+4] accesses the element at index 5. Negative indexing is useful for accessing elements from the end without calculating the length explicitly. For example, list1[-1] returns the last element. This indexing mechanism allows flexible access to list elements for reading or modification.
- List indices start at 0 for the first element.
- Negative indices access elements from the end (-1 is last element).
- Accessing an invalid index raises IndexError.
- len() function returns the length of the list.
- Indices can be expressions evaluating to integers.
- Indexing allows both forward and backward traversal.
- 📌 Indexing: Accessing elements by their position starting at 0.
- 📌 Negative Indexing: Accessing elements from the end using negative indices.
- 📌 len(): Built-in function to get the number of elements in a list.
9.1.2 Lists are Mutable
Concept9.1.2 Lists are Mutable
Lists in Python are mutable, which means their elements can be changed after the list has been created. This is a key difference from strings, which are immutable. Mutability allows us to modify, add, or remove elements in a list without creating a n
Practice Questions — Lists
Includes NCERT exercise questions with answers
Q1.1. What will be the output of the following statements? i. ``` list1 = [12,32,65,26,80,10] list1.sort() print(list1) ``` ii. ``` list1 = [12,32,65,26,80,10] sorted(list1) print(list1) ``` iii. ``` list1 = [1,2,3,4,5,6,7,8,9,10] list1[::-2] list1[:3] + list1[3:] ``` iv. ``` list1 = [1,2,3,4,5] list1[len(list1)-1] ```
Answer:
i. The list1.sort() method sorts the list in place and returns None. So after sorting, list1 becomes [10, 12, 26, 32, 65, 80]. The print statement outputs: [10, 12, 26, 32, 65, 80] ii. The sorted(list1) function returns a new sorted list but does not change list1 itself. Since the returned sorted list is not assigned, list1 remains unchanged. So print(list1) outputs: [12, 32, 65, 26, 80, 10] iii. list1[::-2] returns a slice of list1 starting from the end, stepping backwards by 2 elements: list1 = [1,2,3,4,5,6,7,8,9,10] list1[::-2] = [10,8,6,4,2] list1[:3] + list1[3:] concatenates the first 3 elements and the rest of the list, which is the entire list itself: list1[:3] = [1,2,3] list1[3:] = [4,5,6,7,8,9,10] So the concatenation is the original list: [1,2,3,4,5,6,7,8,9,10] iv. list1[len(list1)-1] accesses the last element of the list: list1 = [1,2,3,4,5] len(list1) = 5 list1[4] = 5 So output is: 5
Explanation:
Step-by-step: (i) list1.sort() sorts the list in place. (ii) sorted(list1) returns a new sorted list but does not modify list1. (iii) list1[::-2] slices the list from end to start with step -2. list1[:3] + list1[3:] concatenates the entire list. (iv) list1[len(list1)-1] accesses the last element by index.
Q2.2. Consider the following list `myList`. What will be the elements of `myList` after the following two operations: ``` myList = [10,20,30,40] i. myList.append([50,60]) ii. myList.extend([80,90]) ```
Answer:
Initially, myList = [10, 20, 30, 40] i. myList.append([50,60]) adds the entire list [50,60] as a single element at the end. So myList becomes: [10, 20, 30, 40, [50, 60]] ii. myList.extend([80,90]) adds each element of the list [80,90] individually to the end of myList. So myList becomes: [10, 20, 30, 40, [50, 60], 80, 90]
Explanation:
append() adds its argument as a single element to the end of the list. extend() iterates over its argument and adds each element individually to the list.
Q3.3. What will be the output of the following code segment: ```python myList = [1,2,3,4,5,6,7,8,9,10] for i in range(0,len(myList)): if i%2 == 0: print(myList[i]) ```
Answer:
The code iterates over indices from 0 to 9 (length of myList - 1). For each index i, if i is even (i%2 == 0), it prints myList[i]. Indices 0,2,4,6,8 are even. myList[0] = 1 myList[2] = 3 myList[4] = 5 myList[6] = 7 myList[8] = 9 Output: 1 3 5 7 9
Explanation:
The for loop checks each index i. If i is even, it prints the element at that index. Thus elements at even indices are printed.
Q4.4. What will be the output of the following code segment: a. myList = [1,2,3,4,5,6,7,8,9,10] del myList[3:] print(myList) b. myList = [1,2,3,4,5,6,7,8,9,10] del myList[:5] print(myList) c. myList = [1,2,3,4,5,6,7,8,9,10] del myList[:2] print(myList)
Answer:
a. del myList[3:] deletes elements from index 3 to end: Original list: [1,2,3,4,5,6,7,8,9,10] After deletion: [1,2,3] b. del myList[:5] deletes elements from start to index 4: Original list: [1,2,3,4,5,6,7,8,9,10] After deletion: [6,7,8,9,10] c. del myList[:2] deletes first two elements: Original list: [1,2,3,4,5,6,7,8,9,10] After deletion: [3,4,5,6,7,8,9,10]
Explanation:
del with slice deletes specified elements from the list. Indices are zero-based and slicing is inclusive of start index and exclusive of end index.
Q5.5. Differentiate between append() and extend() functions of list.
Answer:
append() adds its argument as a single element at the end of the list. Example: myList = [1,2,3] myList.append([4,5]) myList becomes [1,2,3,[4,5]] extend() iterates over its argument and adds each element individually to the list. Example: myList = [1,2,3] myList.extend([4,5]) myList becomes [1,2,3,4,5]
Explanation:
append() treats the argument as a single element. extend() treats the argument as an iterable and adds each element separately.
Q6.6. Consider a list: ```txt list1 = [6,7,8,9] ``` What is the difference between the following operations on list1: a. list1 * 2 b. list1 *= 2 c. list1 = list1 * 2
Answer:
a. list1 * 2 returns a new list which is list1 repeated twice: [6,7,8,9,6,7,8,9] b. list1 *= 2 modifies list1 in place by repeating its elements twice. After this operation, list1 becomes: [6,7,8,9,6,7,8,9] c. list1 = list1 * 2 creates a new list by repeating list1 twice and assigns it back to list1. This is similar to (a) but list1 now refers to the new list: [6,7,8,9,6,7,8,9] Note: In (a), list1 remains unchanged unless assigned. In (b), list1 is modified in place. In (c), list1 is reassigned to a new list.
Explanation:
The * operator repeats the list. *= modifies the list in place. = assigns a new list to the variable.
Q7.7. The record of a student (Name, Roll No., Marks in five subjects and percentage of marks) is stored in the following list: ```txt stRecord = ['Raman','A-36',[56,98,99,72,69], 78.8] ``` Write Python statements to retrieve the following information from the list `stRecord`. - a) Percentage of the student - b) Marks in the fifth subject - c) Maximum marks of the student - d) Roll no. of the student - e) Change the name of the student from 'Raman' to 'Raghav'
Answer:
Given: stRecord = ['Raman','A-36',[56,98,99,72,69], 78.8] Python statements: a) Percentage of the student: percentage = stRecord[3] print(percentage) # Output: 78.8 b) Marks in the fifth subject: fifth_mark = stRecord[2][4] print(fifth_mark) # Output: 69 c) Maximum marks of the student: max_mark = max(stRecord[2]) print(max_mark) # Output: 99 d) Roll no. of the student: roll_no = stRecord[1] print(roll_no) # Output: 'A-36' e) Change the name of the student from 'Raman' to 'Raghav': stRecord[0] = 'Raghav' print(stRecord) # Output: ['Raghav', 'A-36', [56, 98, 99, 72, 69], 78.8]
Explanation:
The list stRecord stores data as: Index 0: Name Index 1: Roll No. Index 2: List of marks Index 3: Percentage Access elements using indexing. Modify name by assigning new value to index 0.
Q8.Write a program to find the number of times an element occurs in the list.
Answer:
To find the number of times an element occurs in a list, we can use the count() method of the list. For example: ```python # Sample list lst = [1, 2, 3, 2, 4, 2, 5] # Element to count element = 2 # Count occurrences count = lst.count(element) print(f"Element {element} occurs {count} times in the list.") ``` This program will output: Element 2 occurs 3 times in the list.
Explanation:
The list method count() returns the number of times the specified element appears in the list. We simply call lst.count(element) to get the count.
All 11 Chapters in Computer Science
Computer Science · Class 11