Wednesday, 23 July 2025

Python List Methods

 List Methods :


lst = [1, 2, 3]


lst.append(4)         # [1, 2, 3, 4]

lst.extend([5, 6])    # [1, 2, 3, 4, 5, 6]

lst.insert(1, 10)     # [1, 10, 2, 3, ...]

lst.remove(10)        # Removes first match

lst.pop()             # Removes last and returns it

lst.pop(1)            # Removes and returns index 1

lst.clear()           # []

lst.index(3)          # Returns index of 3

lst.count(2)          # Count of 2s

lst.sort()            # In-place sort

lst.reverse()         # In-place reverse

sorted(lst)           # Returns sorted copy


No comments:

Post a Comment

Python Tuple , Set and Dict

 Tuple Methods : t = (1, 2, 3) t.count(2)            # 1 t.index(3)            # 2 #Set methods : s = {1, 2, 3} s.add(4)              # {1, ...