0% found this document useful (0 votes)
5 views23 pages

Master Python Lists

This document is a comprehensive cheat sheet for mastering Python lists, covering creation, access, modification, addition, removal, searching, sorting, and looping. It includes examples of list comprehensions, nested lists, built-in functions, and operators, along with advanced hints for using Python effectively. The guide is designed for beginners and intermediate programmers to quickly grasp essential list operations in Python.

Uploaded by

deepikaloya.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

Master Python Lists

This document is a comprehensive cheat sheet for mastering Python lists, covering creation, access, modification, addition, removal, searching, sorting, and looping. It includes examples of list comprehensions, nested lists, built-in functions, and operators, along with advanced hints for using Python effectively. The guide is designed for beginners and intermediate programmers to quickly grasp essential list operations in Python.

Uploaded by

deepikaloya.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🐍

Master Python Lists


(In Minutes!)

Your Ultimate Cheat Sheet
for Python Lists & Operations

✅ Quickly understand lists, slicing, loops…


✅ Perfect for beginners and intermediate programmers

Swipe to Start→
🧱 1. Create Lists
# Empty list​
a = []​

# List with elements​
b = [1, 2, 3, "hello", 4.5, True]​

# From another iterable​
c = list(range(5)) # [0, 1, 2, 3, 4]​
d = list((1, 2, 3)) # [1, 2, 3]
📥 2. Access Elements
my_list = [10, 20, 30, 40, 50]​

# first element​
my_list[0] # 10​

# last element​
my_list[-1] # 50​

# slice from index 1 up to (but not
including) 4​
my_list[1:4] # [20, 30, 40]​

# reversed list using slicing​
my_list[::-1] # [50, 40, 30, 20, 10]
✏️ 3. Modify Elements
my_list = [10, 20, 30, 40, 50]​

my_list[1] = 200
# Changes the element at index 1 (second
element) from 20 → 200​

my_list[2:4] = [300, 400]
# Replaces elements at index 2 and 3
(30, 40) → 300, 400​

print(my_list) # Output: [10, 200, 300,
400, 50]
➕ 4. Add Elements
my_list = [10, 200, 300, 400, 50]​

my_list.append(60)
# add at end

my_list.insert(2, 25)
# add at index 2

my_list.extend([70, 80, 90])
# add multiple at end​

print(my_list)​
# Output: [10, 200, 25, 300, 400, 50,
60, 70, 80, 90]
➖ 5. Remove Elements
my_list = [10, 200, 300, 400, 50]​

my_list.remove(200) # remove value​
my_list.pop() # remove last​
my_list.pop(1) # remove by index​
del my_list[0] # delete by index​
del my_list[1:3] # delete slice​
my_list.clear() # remove all
🔍 6. Search / Check
nums = [10, 20, 30, 20]​

20 in nums # True​
[Link](20) # 2​
[Link](30) # 2
⚙️ 7. Sort and Reverse
nums = [3, 1, 4, 2]​

[Link]()
# sort ascending in-place

[Link](reverse=True)
# sort descending in-place

[Link]()
# reverse in-place

new_list = sorted(nums)
# returns a new sorted list
🔁 8. Looping
for item in nums:
# simple loop​
print(item)​

for i, val in enumerate(nums):
# index + value​
print(i, val)
🧮 9. Mathematical Operations
(with numbers)

nums = [1, 2, 3, 4, 5]​



sum(nums) # 15​
max(nums) # 5​
min(nums) # 1​
len(nums) # 5
🧩 10. Copy and Combine
a = [1, 2]​
b = [3, 4]​

a + b # concatenate -> [1,2,3,4]​
a * 2 # repeat -> [1,2,1,2]
copy = [Link]() # shallow copy​


# For deep copying nested lists:​
import copy​
deep = [Link](nested_list)
🧠 11. List Comprehensions
# simple​
squares = [x**2 for x in range(5)]​

# with condition​
evens = [x for x in range(10) if x % 2
== 0]​

# with if-else​
labels = ["even" if x%2==0 else "odd"
for x in range(6)]​

# flatten 2D list​
matrix = [[1,2], [3,4]]​
flat = [v for row in matrix for v in
row]​

# nested with condition​
pairs = [(i,j) for i in range(3) for j
in range(3) if i!=j]
🗂️ 12. Nested Lists
# 2D list (list of lists)​
matrix = [​
[1, 2, 3],​
[4, 5, 6]​
]​

matrix[0][1] # access element → 2​
matrix[1]
# access entire second row → [4, 5, 6]
🧰 13. Useful Built-in Functions
& Tools

Function / Tool Purpose / Example

len(lst) length

sum(lst) sum numeric elements

sorted(lst) sorted copy

any(lst) True if any truthy

all(lst) True if all truthy

reversed(lst) iterator reversing list

zip(...) combine elementwise

enumerate(...) index + element


Function / Tool Purpose / Example

map(func, seq) apply function lazily


(wrap with list() to get
list)

filter(pred, seq) filter items (wrap with


list())

reduce (functools) accumulate/reduce to


single value

id(obj) identity (memory


address)

type(obj) type of object

dir(obj) list attributes/methods

help(obj) get documentation

isinstance(x, type) type check


Examples:
list(map(str, [1,2,3]))
# ['1','2','3']

list(filter(lambda x: x>2, [1,2,3,4]))
# [3,4]​

id(a), type(a)

dir(list) # show list methods​

help([Link])

isinstance([1,2], list) # True


🔁 14. Operators on Lists
●​+ concatenates lists.

●​* repeats a list.

●​in / not in membership tests.

●​== / != compare element-wise


equality (order & values).

●​<, >, <=, >= lexicographical


comparisons (compare elements
left-to-right).​
Example: [1, 2] < [1, 3] → True because
2 < 3 at first differing index.

●​is / is not check identity (same object).


Examples:

[1,2] + [3] # [1,2,3]​


[1]*3 # [1,1,1]​
2 in [1,2,3] # True​
[1,2] == [1,2] # True​
a = [1,2]; b = a; a is b # True

# '-' not supported for lists​


# use list comprehension to remove items​
a = [1, 2, 3, 4]​
to_remove = [2, 4]​

result = [x for x in a if x not in
to_remove] # [1, 3]
🧪 15. Conversion
tuple(my_list)
# list → tuple

set(my_list)
# list → set (removes duplicates, no
order)

",".join(["a", "b", "c"])
# list → string 'a,b,c'

list(map(int, "1 2 3".split()))
# string → list of ints [1, 2, 3]
🔬 16. Identity vs Equality,
Shallow vs Deep Copy
●​a == b checks equality of contents.​

●​a is b checks whether they are the


same object.​

●​copy() / slicing (lst[:]) create shallow


copies; nested objects still shared.​

●​[Link]() creates an
independent copy recursively.
🧭 17. Small Advanced Hints
●​map() and filter() return iterators in
Python 3 → use list() to convert.

●​Use itertools for infinite sequences,


combinations, permutations, product,
accumulate.

●​For element-wise math on lists, use


numpy arrays or list comprehensions.
🏁 That’s a Wrap!
💡 You just mastered everything
●​
about Python Lists — in minutes!​
From creation ➡️ slicing ➡️ loops ➡️
comprehensions ➡️ and beyond.

📘 Keep this cheat sheet handy


●​
— it’s your mini reference for
interviews, projects, and coding
sessions.
🤝 Let’s Connect
👋 Found this helpful?
Drop a ❤️ 🔁
or to share it with others!

📘 Follow me on LinkedIn and Medium


for more quick tips on Python & AI.

You might also like