Common Data Structure Methods
Common Data Structure Methods
Data structures come with built-in tools called methods.
Methods help you add data, remove data, search data, sort data, and more.
Learning common methods will make your Python work faster and easier.
What Is a Method?
A method is a function attached to an object.
You run it using a dot .
name = "python"
print(name.upper())
Output
PYTHON
Here:
upper()is a method- It works on the string stored in
name
List Methods
append()
Adds one item to the end.
items = ["Pen", "Book"]
items.append("Bag")
print(items)
Output
['Pen', 'Book', 'Bag']
insert()
Adds an item at a position.
items = ["Pen", "Bag"]
items.insert(1, "Book")
print(items)
Output
['Pen', 'Book', 'Bag']
remove()
Removes by value.
items = ["Pen", "Book", "Bag"]
items.remove("Book")
print(items)
Output
['Pen', 'Bag']
pop()
Removes by position.
items = ["Pen", "Book", "Bag"]
items.pop(0)
print(items)
Output
['Book', 'Bag']
sort()
Sorts the list.
numbers = [3, 1, 2]
numbers.sort()
print(numbers)
Output
[1, 2, 3]
reverse()
Reverses the list.
numbers = [1, 2, 3]
numbers.reverse()
print(numbers)
Output
[3, 2, 1]
Dictionary Methods
get()
Reads a value safely.
user = {"name": "Tom"}
print(user.get("name"))
print(user.get("age"))
Output
Tom
None
keys()
Shows all keys.
print(user.keys())
Output
dict_keys(['name'])
values()
Shows all values.
print(user.values())
Output
dict_values(['Tom'])
items()
Shows key-value pairs.
print(user.items())
Output
dict_items([('name', 'Tom')])
pop()
Removes by key.
user.pop("name")
print(user)
Output
{}
Set Methods
add()
Adds one item.
tags = {"python"}
tags.add("coding")
print(tags)
update()
Adds many items.
tags.update(["course", "learn"])
print(tags)
remove()
Removes an item.
tags.remove("coding")
print(tags)
discard()
Safe remove.
tags.discard("missing")
print(tags)
String Methods
upper()
text = "hello"
print(text.upper())
Output
HELLO
lower()
print(text.lower())
Output
hello
title()
print(text.title())
Output
Hello
replace()
print(text.replace("hello", "hi"))
Output
hi
split()
words = "learn python now"
print(words.split())
Output
['learn', 'python', 'now']
Why Methods Matter
Methods save time and make code cleaner.
Instead of building everything from scratch, Python gives ready-made tools.
Code Along
cities = ["Toronto", "New York"]
cities.append("Chicago")
cities.sort()
print(cities)
Output
['Chicago', 'New York', 'Toronto']
Mini Challenge
Build a data tool.
Steps:
- Create a list:
3, 1, 2 - Sort it
- Add
4 - Create a dictionary:
name: Tom - Print the keys
- Create a string:
hello world - Print it in uppercase
Expected output:
[1, 2, 3, 4]
dict_keys(['name'])
HELLO WORLD
Real World Use Case
Programs use methods every day for text cleaning, updating records, sorting products, and managing data.
Quiz
- What is a method?
- Which symbol is used to call a method?
- Which list method sorts data?
- Which dictionary method returns all keys?
Assignment
Create your own examples using one list method, one dictionary method, and one string method.
Summary
You learned common methods for lists, dictionaries, sets, and strings that help manage data quickly and cleanly.