🔁 Top 30 Python List Methods You Should Know 🧠📋 1. append() – Add element to the end lst = [ 1 , 2 ] lst.append( 3 ) # Output: [1, 2, 3] 2. extend() – Add multiple elements from another list lst = [ 1 , 2 ] lst.extend([ 3 , 4 ]) # Output: [1, 2, 3, 4] 3. insert() – Insert element at specific index lst = [ 1 , 3 ] lst.insert( 1 , 2 ) # Output: [1, 2, 3] 4. remove() – Remove first occurrence of a value lst = [ 1 , 2 , 3 ] lst.remove( 2 ) # Output: [1, 3] 5. pop() – Remove element at index (default last) lst = [ 1 , 2 , 3 ] lst.pop( 1 ) # Output: 2, list becomes [1, 3] 6. index() – Find index of first occurrence lst = [ 10 , 20 , 30 ] lst.index( 20 ) # Output: 1 7. count() – Count occurrences of value lst = [ 1 , 1 , 2 ] lst.count( 1 ) # Output: 2 8. sort() – Sort list in ascending order (in-place) lst = [ 3 , 1 , 2 ] lst.sort() # Output: [1, 2, 3] 9. reverse() – Reverse list (in-place) lst = [ 1 , 2 , 3 ] lst.reverse() # Output: [3, 2,...
Comments
Post a Comment