Python Lists - Complete Reference
PYTHON LISTS - COMPLETE REFERENCE
1. LIST CREATION
empty_list = []
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", 3.14, True, None]
nested = [[1, 2], [3, 4], [5, 6]]
2. LIST LENGTH
len(fruits)
3. ACCESS ELEMENTS
fruits[0], fruits[-1], nested[1][0]
4. MODIFY ELEMENTS
fruits[1] = "blueberry"
5. ADD ELEMENTS
append(), insert(), extend()
6. REMOVE ELEMENTS
remove(), pop(), del, clear()
7. LIST SLICING
nums[1:4], nums[:3], nums[::-1], nums[::2]
8. COPYING LISTS
copy(), list(), slicing [:]
9. COMBINE LISTS
+, extend()
10. SEARCHING IN LIST
in, index(), count()
11. SORTING & REVERSING
sort(), reverse()
12. BUILT-IN FUNCTIONS
min(), max(), sum()
13. LOOPING
for item in list, enumerate()
14. LIST COMPREHENSION
[x**2 for x in range(5)], [x for x in range(10) if x%2==0]
15. NESTED LIST COMPREHENSION
[item for sublist in nested for item in sublist]
16. ADVANCED FUNCTIONS
map(), filter(), zip()
17. LIST REPLICATION
[0] * 5
18. SHALLOW VS DEEP COPY
[Link](), [Link]()
19. REMOVE DUPLICATES
list(set(list))
20. PERFORMANCE TIPS
List comprehension faster than append()
21. UNPACKING LISTS
a, b, c = [1, 2, 3]
first, *rest = [1, 2, 3, 4]
22. ANY / ALL CHECK
any(), all()
23. MERGE LIST OF LISTS
sum(list_of_lists, [])
24. ROTATE LIST
nums[rotate:] + nums[:rotate]
25. FIND DUPLICATES
[x for x in set(nums) if [Link](x) > 1]
NOTES:
- Lists are mutable.
- Index starts at 0.
- append() is O(1), insert(0,x) is O(n).
- Use lists for mutable data, tuples for fixed data.