CodingNic

Data Structures

Common Data Structure Methods

Data Structures 26 min read

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 .

python
name = "python"
print(name.upper())

Output

text
PYTHON

Here:

  • upper() is a method
  • It works on the string stored in name

List Methods

append()

Adds one item to the end.

python
items = ["Pen", "Book"]
items.append("Bag")

print(items)

Output

text
['Pen', 'Book', 'Bag']

insert()

Adds an item at a position.

python
items = ["Pen", "Bag"]
items.insert(1, "Book")

print(items)

Output

text
['Pen', 'Book', 'Bag']

remove()

Removes by value.

python
items = ["Pen", "Book", "Bag"]
items.remove("Book")

print(items)

Output

text
['Pen', 'Bag']

pop()

Removes by position.

python
items = ["Pen", "Book", "Bag"]
items.pop(0)

print(items)

Output

text
['Book', 'Bag']

sort()

Sorts the list.

python
numbers = [3, 1, 2]
numbers.sort()

print(numbers)

Output

text
[1, 2, 3]

reverse()

Reverses the list.

python
numbers = [1, 2, 3]
numbers.reverse()

print(numbers)

Output

text
[3, 2, 1]

Dictionary Methods

get()

Reads a value safely.

python
user = {"name": "Tom"}

print(user.get("name"))
print(user.get("age"))

Output

text
Tom
None

keys()

Shows all keys.

python
print(user.keys())

Output

text
dict_keys(['name'])

values()

Shows all values.

python
print(user.values())

Output

text
dict_values(['Tom'])

items()

Shows key-value pairs.

python
print(user.items())

Output

text
dict_items([('name', 'Tom')])

pop()

Removes by key.

python
user.pop("name")
print(user)

Output

text
{}

Set Methods

add()

Adds one item.

python
tags = {"python"}
tags.add("coding")

print(tags)

update()

Adds many items.

python
tags.update(["course", "learn"])

print(tags)

remove()

Removes an item.

python
tags.remove("coding")

print(tags)

discard()

Safe remove.

python
tags.discard("missing")
print(tags)

String Methods

upper()

python
text = "hello"
print(text.upper())

Output

text
HELLO

lower()

python
print(text.lower())

Output

text
hello

title()

python
print(text.title())

Output

text
Hello

replace()

python
print(text.replace("hello", "hi"))

Output

text
hi

split()

python
words = "learn python now"
print(words.split())

Output

text
['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

python
cities = ["Toronto", "New York"]
cities.append("Chicago")
cities.sort()

print(cities)

Output

text
['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:

text
[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

  1. What is a method?
  2. Which symbol is used to call a method?
  3. Which list method sorts data?
  4. 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.