Loading...

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 lines
|
10/ 500 tokens
1
fruits = ["apple", "banana", "cherry"]
Code Tools

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
3 lines
|
38/ 500 tokens
1
2
3
fruits[1] = "blueberry"  # Change "banana" to "blueberry"
fruits.append("orange")  # Add new item at the end
fruits.remove("apple")   # Remove "apple"
Code Tools

Prompt

text
1 lines
|
14/ 500 tokens
1
# Write a function that removes duplicates from a list

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

python
2 lines
|
14/ 500 tokens
1
2
def remove_duplicates(lst):
    return list(set(lst))
Code Tools

Explanation

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

Practice

python
2 lines
|
22/ 500 tokens
1
2
nums = [1, 2, 2, 3, 4, 4, 5]
print(remove_duplicates(nums))  # Output: [1, 2, 3, 4, 5]
Code Tools

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 lines
|
8/ 500 tokens
1
person = ("Alice", 25, "Doctor")
Code Tools

Packing and Unpacking

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

Prompt

text
1 lines
|
16/ 500 tokens
1
# Create a tuple and unpack its values into separate variables

Copilot might suggest:

python
6 lines
|
35/ 500 tokens
1
2
3
4
5
6
person = ("Alice", 25, "Doctor")
name, age, profession = person

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

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 lines
|
9/ 500 tokens
1
colors = {"red", "green", "blue"}
Code Tools

Set Operations

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

Prompt

text
1 lines
|
14/ 500 tokens
1
# Write code that finds common elements between two sets

Copilot might suggest:

python
5 lines
|
27/ 500 tokens
1
2
3
4
5
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

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

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 lines
|
13/ 500 tokens
1
student = {"name": "John", "age": 20, "grade": "A"}
Code Tools

Accessing and Updating

python
4 lines
|
30/ 500 tokens
1
2
3
4
print(student["name"])  # John

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

Prompt

text
1 lines
|
16/ 500 tokens
1
# Create a dictionary to store student names and their grades

Copilot might suggest:

python
7 lines
|
26/ 500 tokens
1
2
3
4
5
6
7
grades = {
    "Alice": "A",
    "Bob": "B",
    "Charlie": "C"
}

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

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

text
1 lines
|
27/ 500 tokens
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

No! This lesson explains each data structure from scratch, even if you've never used one before.

Yes! Copilot can help you create and manipulate lists, sets, dictionaries, and more, but you should still understand each structure first.

Read the explanation step by step. You can also ask Copilot to add comments or rewrite in a simpler way.

Absolutely! Experimenting with different values is the best way to learn.

Copilot is a helpful assistant, but make sure you test and understand each output to truly learn and build confidence.

Still have questions?Contact our support team