🐍 List only manipulation

list

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 index

Removing 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 list

Counting & Searching

friends.count("Jim")
# How many times "Jim" appears
 
"Jim" in friends
# True if "Jim" is inside the list

Sorting

friends.sort()
# Sorts list (alphabetical or numeric)
 
friends.sort(reverse=True)
# Sorts descending
 
friends.reverse()
# Reverses the list in-place

Copying 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 random

Pick a Random Element

choices = ["rock", "paper", "scissors"]
result = random.choice(choices)
# Returns one random element from the list

Shuffle 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