Python Lists are ordered collections of items, where each item can be of any data type, and they are enclosed within square brackets []. Lists support various operations such as appending, indexing, slicing, and iterating. They can contain heterogeneous data types, allowing flexibility in storing different kinds of elements like integers, strings, floats, or even other lists (nested lists). In addition, they are mutable, meaning their elements can be changed after creation, making them versatile for dynamic data storage and manipulation. Furthermore, they support indexing and slicing to access elements or sublists; and have their own methods, which we will see below.
# number of elements/items in a list
num = len(basketball_champions)
print(f"Number of elements: {num}") # 40
# last element
try:
print(basketball_champions[40]) # error, indexing start at 0
except IndexError:
print("Oops! Out of Index!")
# last element (again)
print(basketball_champions[39])
#or
print(basketball_champions[num-1])
#or (preferrable)
print(basketball_champions[-1])
# first element
print(basketball_champions[0])
# OUTPUT
"""
Number of elements: 40
Oops! Out of Index!
1
1
1
Los Angeles Lakers
"""
1.2. Multiple Elements (Slicing)
# print first two teams with their championships
print(basketball_champions[0:4])
# print last two teams with their championships
print(basketball_champions[-4:])
# print only teams
print(basketball_champions[::2])
# OUTPUT
"""
['Los Angeles Lakers', 17, 'Boston Celtics', 17]
['Toronto Raptors', 1, 'Denver Nuggets', 1]
['Los Angeles Lakers', 'Boston Celtics', 'Golden State Warriors', 'Chicago Bulls', 'San Antonio Spurs']
"""
2. Updating Lists
2.1. Changing list elements
# change championship for Lakers to 25
basketball_champions[1] = 25
print(basketball_champions[:2])
# increase championship for Denver Nuggets
basketball_champions[-1] = 2
print(basketball_champions[-2:])
# OUTPUT
"""
['Los Angeles Lakers', 25]
['Denver Nuggets', 2]
"""
2.2. Adding and removing elements
# adding Phoenix Suns with 0 championships to our list
basketball_champions += ['Phoenix Suns',0]
print(basketball_champions[-6:])
# 1.Deleting by Index
# a. using del
# deleting 'Phoenix Suns' and its championships value from the list
del basketball_champions[-2:]
print(basketball_champions[-6:])
# b. using .pop()
# deleting the last element will be deleted
basketball_champions.pop() # if no index given, then the last element will be deleted
# or the same as basketball_champions.pop(-1)
print(basketball_champions[-6:])
# Deleting by Value
# removing a specific item (first occurence of that item!)
basketball_champions.remove('Denver Nuggets')
print(basketball_champions[-6:])
# OUTPUT
"""
['Toronto Raptors', 1, 'Denver Nuggets', 2, 'Phoenix Suns', 0]
['Sacramento Kings', 1, 'Toronto Raptors', 1, 'Denver Nuggets', 2]
[1, 'Sacramento Kings', 1, 'Toronto Raptors', 1, 'Denver Nuggets']
['Dallas Mavericks', 1, 'Sacramento Kings', 1, 'Toronto Raptors', 1]
"""
In python del isa keyword and remove(), pop() are in-built methods (see section #3 for more methods). The difference between del and pop() is that even though they both delete the values at a particular index, pop() returns the deleted value:
del basketball_champions[0]
# No OUTPUT
basketball_champions.pop(0)
# OUTPUT: 'Los Angeles Lakers'
# insert Chicago Bulls and 99 to the list
basketball_champions.insert(0,'Chicago Bulls')
basketball_champions.insert(1,99)
print(basketball_champions[:6])
# retrieve Chicago Bulls index
print(basketball_champions.index('Chicago Bulls'))
# retrieve Chicago Bulls' original index
print(basketball_champions.index('Chicago Bulls',1))
# count the frequency of a specific value
print(f'Number of "Chicago Bulls" entries: {basketball_champions.count("Chicago Bulls")}')
print(f"Number of teams with single championships: {basketball_champions.count(1)}")
# OUTPUT
"""
['Chicago Bulls', 99, 'Los Angeles Lakers', 17, 'Boston Celtics', 17]
0
8
Number of "Chicago Bulls" entries: 2
Number of teams with single championships: 9
"""
# delete Chicago Bulls with 99 championships
basketball_champions.pop(0)
basketball_champions.pop(0)
Lists are great data types to store data; however, they fall short when it comes to arithmetic:
# extract championships only to do arithmetic
champs = basketball_champions[1::2]
print(champs)
# multiply each element with itself
champs * champs
# OUTPUT
"""
[17, 17, 7, 6, 5, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[76], line 4
1 champs = basketball_champions[1::2]
2 print(champs)
----> 4 champs * champs
TypeError: can't multiply sequence by non-int of type 'list'
"""
To do such calculation we need the help of a for loop:
A better approach would be to use the Numpy package! We first convert our list to a numpy array and then can do the multiplication without having use of a for loop.
Python lists are versatile data structures that can hold a collection of elements of different types. They are mutable, meaning we can modify their contents after creation. Lists support indexing and slicing to access elements or sublists and have built-in methods to operate on them more easily. They are dynamic in size and can grow or shrink as needed, making them a fundamental building block for many Python programs.
All being said, because of the nature of the dataset we used (i.e. having only pairs of 'team' and 'championships') it is better if we use another Python's data structure called Dictionary.