The goal of this tutorial is to help you understand and become proficient in working with Lists and Sets, two basic types of collections in many programming languages. We'll explore creating, populating, manipulating, and traversing these structures.
By the end of this tutorial, you should be able to:
- Understand what Lists and Sets are
- Create and populate Lists and Sets
- Manipulate (add, remove, update) items in Lists and Sets
- Traverse or iterate over Lists and Sets
This tutorial assumes you have a basic understanding of programming concepts such as variables, data types, and loops.
A List is an ordered collection of items. Lists are mutable, meaning they can be changed after they are created.
You can create a list by enclosing the items (elements) in square brackets []
, separated by commas. For example:
my_list = [1, 2, 3, 'four', 'five']
You can add elements to a list using the append()
method, remove elements with the remove()
method, and change elements by accessing them directly using their index.
my_list.append('six') # adds 'six' to the end of the list
my_list.remove(1) # removes the first occurrence of 1 from the list
my_list[0] = 'one' # changes the first element (at index 0) to 'one'
A Set is an unordered collection of unique items. Like lists, sets are also mutable.
You can create a set using the set()
function or by enclosing the items in curly braces {}
, separated by commas. For example:
my_set = set([1, 2, 3, 'four', 'five'])
# or
my_set = {1, 2, 3, 'four', 'five'}
You can add elements to a set using the add()
method, remove elements with the remove()
method.
my_set.add('six') # adds 'six' to the set
my_set.remove(1) # removes 1 from the set
# Create a list
fruits = ['apple', 'banana', 'cherry']
# Add element to the list
fruits.append('date')
# Remove element from the list
fruits.remove('banana')
# Print the list
print(fruits) # Output: ['apple', 'cherry', 'date']
# Create a set
fruits = {'apple', 'banana', 'cherry'}
# Add element to the set
fruits.add('date')
# Remove element from the set
fruits.remove('banana')
# Print the set
print(fruits) # Output: {'date', 'apple', 'cherry'}
In this tutorial, we've covered how to create, populate, manipulate, and traverse Lists and Sets. Practice these concepts to solidify your understanding.
Create a list of your five favourite movies. Then:
- Add a sixth movie to your list.
- Remove the third movie from your list.
- Print out the fourth movie on your list.
Create a set of five different types of fruits. Then:
- Add a sixth fruit to your set.
- Remove one fruit from your set.
- Print out all the fruits in your set.
Here are the solutions to the exercises:
# Create a list of favourite movies
movies = ['Movie 1', 'Movie 2', 'Movie 3', 'Movie 4', 'Movie 5']
# Add a movie to the list
movies.append('Movie 6')
# Remove the third movie from the list
movies.remove('Movie 3')
# Print the fourth movie
print(movies[3]) # Output: 'Movie 4'
# Create a set of fruits
fruits = {'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'}
# Add a fruit to the set
fruits.add('Fig')
# Remove a fruit from the set
fruits.remove('Date')
# Print the set of fruits
print(fruits) # Output: {'Apple', 'Banana', 'Cherry', 'Elderberry', 'Fig'}