Sequential Data Types#
Author: Mike Wood
Learning objectives: By the end of this notebook, you should be able to:
Create and access data within sequential data types (lists, tuples, and sets)
Explain the differences between Python’s sequential data types
Call built-in methods on sequential data types and change from one sequential type to another
Lists#
One of the most common sequential data types used in Python is the list. Lists are used to carry groups of objects - primitive data types, objects, or even other lists.
Lists typically hold homogenous data, such as integers or strings, and are delineated by square brackets:
# create a list of integers called my_numbers
my_numbers = [1,2,3,4,5]
# print the list
print(my_numbers)
[1, 2, 3, 4, 5]
# create a list of strings called my_words
my_words = ["This", "is", "a", "list", "of", "words"]
# print the list
print(my_words)
['This', 'is', 'a', 'list', 'of', 'words']
However, lists can mix and match data types, storing a variety of objects:
# create a list called mixed_list which has an integer, a string, and another list
mixed_list = [1,"a",["Another","list"]]
# print the list
print(mixed_list)
[1, 'a', ['Another', 'list']]
As we can see, lists are extremely flexible objects!
To check how many objects are in a list, we can use the len method (short for length):
print(len(mixed_list))
3
To access an object within a list, use the index of the item you are interested in:
# get the 3rd word in the my_words list and store it to a variable
third_word = my_words[2]
# print the variable
print(third_word)
a
If you’re interested in more than one element, you can use a range of value with the start index up to the last index, for example:
# print the words "is" "a" and "list" from the my_words list
print(my_words[1:4])
['is', 'a', 'list']
To determine the index of a given object in a list, use the list’s .index method
# get the index of the word 'list' in the my_words list
list_index = my_words.index('list')
# print the index
print(list_index)
3
🤔 Mini-Exercise#
Goal: Print the string representation of a given integer.
Method: Use the .index method to get the location of the given number in the integer list and use the index to retrieve the string representation from the string list. Be sure to print out your string.
# integers in the Fibonacci sequence
int_list = [1, 2, 3, 5, 8, 13, 21, 34, 55]
# string representation of integers in the Fibonacci sequence
str_list = ['one', 'two', 'three', 'five', 'eight', 'thirteen',
'twenty-one', 'thirty-four', 'fifty-five']
# get the string repesentation of this integer
given_integer = 3
💡 Solution#
# enter solution here
string_representation = str_list[int_list.index(given_integer)]
print(string_representation)
three
Common List Methods:#
The following table summarizes some common list methods:
Method |
Description |
|---|---|
append() |
Adds an element at the end of the list |
clear() |
Removes all the elements from the list |
copy() |
Returns a copy of the list |
count() |
Returns the number of elements with the given value |
extend() |
Add the elements of a list, to the end of the current list |
index() |
Returns the index of the first element with the specified value |
insert() |
Adds an element at the given position |
pop() |
Removes the element at the given position |
remove() |
Removes the first item with the given value |
reverse() |
Reverses the order of the list |
sort() |
Sorts the list |
Tuples#
Tuples are very similar to lists - they are groups of objects that can be of any size. Tuples are delineated by round parentheses:
# create a tuple called my_numbers_tuple which has the integers 1-5
my_numbers_tuple = (1, 2, 3, 4, 5)
# print the tuple
print(my_numbers_tuple)
(1, 2, 3, 4, 5)
Similar to lists, tuples can mix and match data types:
# create a tuple which has an integer, a string, and a list
my_mixed_tuple = (1,"a",["Another","list"])
# print the tuple
print(my_mixed_tuple)
(1, 'a', ['Another', 'list'])
Tuples have many of the same methods as lists:
# print the first entry in my_numbers_tuple
print(my_numbers_tuple[0])
# get the number of elements in my_numbers_tuple
print(len(my_numbers_tuple))
1
5
The main difference between tuples and lists are that tuples are immutable - that is, they cannot be changed once they are created. Try this for yourself:
# try to change the last element in my_numbers_tuple to the integer 6
# by uncommenting the line below
# my_numbers_tuple[-1] = 6
# what happens?
Lists and tuples can be converted to one another by wrapping them in the desired sequence type.
# create a list of objects
my_list = [1,2,3,4]
print(my_list)
print(type(my_list))
print(' ')
# convert the list to a tuple and print its type
my_list = tuple(my_list)
print(my_list)
print(type(my_list))
print(' ')
# convert the tuple back to a list and print its type
my_list = list(my_list)
print(my_list)
print(type(my_list))
[1, 2, 3, 4]
<class 'list'>
(1, 2, 3, 4)
<class 'tuple'>
[1, 2, 3, 4]
<class 'list'>
Sets#
Sets are similar to lists and tuples except that they only store unique values. They are initialized with curly brackets {}
# initialize a set of numbers
my_set_1 = {1,2,3,4}
print(my_set_1)
# initialize a set of number with duplicates - what happens?
my_set_2 = {1,2,3,4,4,4}
print(my_set_2)
{1, 2, 3, 4}
{1, 2, 3, 4}
Just like tuples, sets can be converted to lists ands vice verse. However, the uniqueness feature of sets means that duplicates will be removed. Try this for yourself:
# make a list with duplicates
my_duplicate_list = [1,2,2,3,3,4,4]
print(my_duplicate_list)
# convert the list to a set
my_duplicate_list = set(my_duplicate_list)
print(my_duplicate_list)
[1, 2, 2, 3, 3, 4, 4]
{1, 2, 3, 4}
🤔 Mini-Exercise#
Goal: Get a list of unqiue values in a list with duplicates.
# list of student surnames with duplicates
student_surnames = ['Wood', 'Chen', 'Nguyen', 'Ho', 'Koh', 'Jones', 'Nguyen', 'Jones',
'Nguyen', 'Arora', 'Mamidi', 'Nguyen', 'Phan', 'Wood']
💡 Solution#
# make a list of unique student surnames
# print the surnames in alphabetical order
unique_student_surnames = sorted(list(set(student_surnames)))
print(unique_student_surnames)
['Arora', 'Chen', 'Ho', 'Jones', 'Koh', 'Mamidi', 'Nguyen', 'Phan', 'Wood']