Data Structures in Python with Copilot Prompts

In this lesson, you'll learn how to store and organize data using Python’s built-in data structures: lists, tuples, sets, and dictionaries.

Using Copilot prompts, you’ll not only see how they work but also learn to write code confidently!

Lists

Lists are like flexible containers where you can store multiple items, such as numbers or strings. They are ordered and changeable (mutable).

Creating Lists

python
1
fruits = ["apple", "banana", "cherry"]

You can store any type of data: numbers, strings, even other lists!

Indexing and Slicing

  • fruits[0]: "apple" (first item)
  • fruits[-1]: "cherry" (last item)
  • fruits[1:3]: ["banana", "cherry"] (slice from index 1 to 2)

Modifying Lists

python
1
2
3
fruits[1] = "blueberry"  # Change "banana" to "blueberry"
fruits.append("orange")  # Add new item at the end
fruits.remove("apple")   # Remove "apple"

Prompt

1
# Write a function that removes duplicates from a list

When you type this and start the function, Copilot might suggest:

python
1
2
def remove_duplicates(lst):
    return list(set(lst))

Explanation

  • set(lst): Converts the list to a set (which only keeps unique items).
  • list(...): Converts it back to a list.

Practice

python
1
2
nums = [1, 2, 2, 3, 4, 4, 5]
print(remove_duplicates(nums))  # Output: [1, 2, 3, 4, 5]

Tuples

Tuples are like lists, but immutable, meaning you can’t change them after creation. They are great for fixed data.

Creating Tuples

python
1
person = ("Alice", 25, "Doctor")

Packing and Unpacking

  • Packing: Putting values together in a tuple.
  • Unpacking: Splitting tuple values into separate variables.

Prompt

1
# Create a tuple and unpack its values into separate variables

Copilot might suggest:

python
1
2
3
4
5
6
person = ("Alice", 25, "Doctor")
name, age, profession = person

print(name)       # Alice
print(age)        # 25
print(profession) # Doctor

Explanation

  • name, age, profession = person: Breaks the tuple into three separate variables in one line.

Practice

Try creating a tuple with your favorite movie, year, and rating, then unpack it!

Sets

Sets are unordered collections of unique items. They are useful for removing duplicates and doing set operations like finding common elements.

Creating Sets

python
1
colors = {"red", "green", "blue"}

Set Operations

  • intersection(): Common items.
  • union(): All items from both sets.
  • difference(): Items only in one set.

Prompt

1
# Write code that finds common elements between two sets

Copilot might suggest:

python
1
2
3
4
5
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

common = set1.intersection(set2)
print(common)  # Output: {3, 4}

Explanation

  • intersection(): Finds values present in both sets.

Practice

Try finding items only in set1 but not in set2 using difference().

Dictionaries

Dictionaries store key-value pairs, like a real-life dictionary where a word (key) maps to a definition (value).

Creating Dictionaries

python
1
student = {"name": "John", "age": 20, "grade": "A"}

Accessing and Updating

python
1
2
3
4
print(student["name"])  # John

student["age"] = 21     # Update age
student["city"] = "NY"  # Add new key-value pair

Prompt

1
# Create a dictionary to store student names and their grades

Copilot might suggest:

python
1
2
3
4
5
6
7
grades = {
    "Alice": "A",
    "Bob": "B",
    "Charlie": "C"
}

print(grades["Alice"])  # Output: A

Explanation

  • Keys ("Alice", "Bob", etc.) represent student names.
  • Values ("A", "B", etc.) represent grades.

Practice

  • Add another student to the dictionary.
  • Change Bob’s grade to "A+".

Key Takeaways

  • Lists are ordered, changeable collections.
  • Tuples are ordered but unchangeable.
  • Sets are unordered and contain unique elements.
  • Dictionaries map keys to values and are great for lookups.
  • Copilot prompts help you experiment and understand each structure easily.

Mini Challenge

Prompt

1
# Create a dictionary of five countries and their capitals. Then, write a function to print all capitals.

Ask Copilot to generate the dictionary and the function, then run it to see your results!

Frequently Asked Questions