⚡0 XP🔥0

Ch 8 · Lists | सूची

Lesson 8 of 11 ⏱ 3 min read ✎ 1 exercises ❓ 5-question quiz ▶ Runs Python

Lists - indexing, slicing, operations, traversal, built-in functions and methods, nested lists, and programs (search, max/min, frequency).

Lists#

Ordered, mutable collection: L = [10, 20, 30]. Indexing and slicing work like strings. Elements can be changed: L[0] = 5.

▶ Try it YourselfPython
Ctrl + Enter to run
Method/function Does
append(x) add at end
extend(list) add many
insert(i, x) add at position
remove(x) remove first x
pop(i) remove and return (last by default)
sort(), reverse() in place
count(x), index(x) search
len, max, min, sum, sorted built-in functions
हिंदी में समझें

सूची (list) परिवर्तनशील होती है - इसके तत्व बदले, जोड़े या हटाए जा सकते हैं। append() अंत में जोड़ता है, insert() किसी स्थान पर, remove() मान से हटाता है और pop() स्थान से हटाकर लौटाता है।

Nested lists and copying#

M = [[1, 2], [3, 4]]; M[1][0] is 3. Assignment B = A makes both names refer to the same list; use A.copy() or list(A) for a separate copy.

▶ Try it YourselfPython
Ctrl + Enter to run

Exam tips#

  • Programs: linear search, frequency of elements, separate even/odd, find second largest.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Second largest

Given list L, print the second largest distinct value.

Expected output:
60

Quick Quiz

Q1.

Lists are

Q2.

L.pop() removes

Q3.

[1, 2] + [3] gives

Q4.

If B = A, changing A also changes B because

Q5.

Method to add multiple elements is