🐍 List only manipulation
Adding Elements
friends.append("Max")
# Adds a single element to the end
friends.extend(numbers)
# Adds all elements from another list
friends.insert(1, "Jonny")
# Inserts at a specific indexRemoving Elements
friends.remove("Tom")
# Removes the first matching value
friends.pop()
# Removes and returns the last element
friends.pop(2)
# Removes and returns element at index 2
friends.clear()
# Empties the entire listCounting & Searching
friends.count("Jim")
# How many times "Jim" appears
"Jim" in friends
# True if "Jim" is inside the listSorting
friends.sort()
# Sorts list (alphabetical or numeric)
friends.sort(reverse=True)
# Sorts descending
friends.reverse()
# Reverses the list in-placeCopying Lists
friends2 = friends.copy()
# Makes a shallow copy of the list**Random Functions with Lists **
These tools from the random module help you pick or mix values inside lists.
Setup
import randomPick a Random Element
choices = ["rock", "paper", "scissors"]
result = random.choice(choices)
# Returns one random element from the listShuffle a List
cards = [1,2,3,4,5,6,7,8,9,10, "J", "Q", "K", "A"]
random.shuffle(cards)
# Shuffles the list *in place*
# e.g. ["Q", 7, 1, "A", 4, ...]Pick Multiple Random Elements
random.sample(cards, 3)
# Returns 3 unique random elements → does NOT modify the list