Python Cheat Sheet:
Lists, Dictionaries, and Tuples

This cheat sheet was originally written by A.M. Bosworth as an extra credit assignment for Prof. Zareh Gorgian's Winter 2020 CIS 012 class at Pasadena City College.


Lists:

Lists are the simplest way to group items in Python. Lists remember the order in which items were placed inside of them. The first element is at index 0, which is the first place in the list.

A list with 5 items has indexes from 0 to 4, with the fifth item at index 4.

Creating a List

You can declare an empty list like this:

listVariable = []

Or you can put stuff in the list at the beginning like this:

listVariable = [item, otherItem, oneMoreItem]

Modifying Lists

You can add more stuff to a list like this:

listVariable = [1,2,3,4]
item = 5
listVariable.append(item) # This is how you do it!

You can remove stuff from a list like this:

listVariable = [1,2,3,4]
item = 3
listVariable.remove(item) # This is how you do it
# Result inside listVariable will be: [1,2,4]

You can find items using their index like this:

listVariable = [1,2,3,4]
item = listVariable[0] # This is how you do it
# Result inside item will be: 1

You can change values if you know their index:

listVariable = [1,2,3,4]
listVariable[1] = 5 # This is how you do it
# Result inside listVariable will be: [1,5,3,4]

Lists and Loops

You can use loops to iterate through lists:

for i in listVariable:
    print(i)
# Will print each item in listVariable on its own line

But you need to be more specific if you want to use loops to modify the list:

for i in range(0,len(listVariable)) :
    listVariable[i] += 1
# If you tried the for declaration in the previous
# example, you'd get an error.

Dictionaries:

Dictionaries are unordered, so you just add things and then they show up in whatever order they feel like showing up in. Unlike lists, you can't find things by index in dictionaries.

Instead, you search by a 'key' to find a 'value'. In fact, every entry in a dictionary MUST be a 'key'-'value' pair.

The 'value' that's attached to a 'key' CAN be a list, though. The 'key', however cannot be a list.

Creating a Dictionary

You can declare an empty dictionary like this:

dictVariable = dict()

Or you can put stuff in the dictionary at the beginning like this:

dictVariable = {'key':'value', 'keyblade':'ultima', 
'nomura':'eccentric'}

Modifying Dictionaries

You can add more stuff to a dictionary like this:

dictVariable = dict()
dictVariable['key'] = 'value' # This is how you do it

You can remove stuff from a dictionary like this:

dictVariable = {'key':'value', 'keyblade':'ultima', 
'nomura':'eccentric'}
dictVariable.pop('nomura') # This is how you do it
# Note that if you have
# personality = dictVariable.pop('nomura')
# personality would then hold 'eccentric' and
# dictVariable would hold
# {'key':'value', 'keyblade':'ultima'}

You can find values using their keys like this:

dictVariable = {'key':'value', 'keyblade':'ultima', 
'nomura':'eccentric'}
personality = dictVariable['nomura'] # This is how you do it
# Result inside personality will be: 'eccentric'

You can change values if you know their key:

dictVariable = {'key':'value', 'keyblade':'ultima', 
'nomura':'eccentric'}
dictVariable['nomura'] = 'awesome' # This is how you do it
# Result inside dictVariable will be:
# {'key':'value', 'keyblade':'ultima', 'nomura':'awesome'}

Dictionaries and Loops

You can use loops to iterate through dictionaries:

dictVariable = {'key':'value', 'keyblade':'ultima', 
'nomura':'eccentric'}
for i in dictVariable:
    print(i + " : " + dictVariable[i])
# Will print each key and value in dictVariable
# separated by " : " and on its own line

You can modify values using loops as per the above example, but you can't modify keys.

Here's a (terrible) example of how to modify keys using for-loops:

dictVariable = {0:'value', 1:'ultima', 2:'eccentric'}
for i in range(0,len(dictVariable)):
    newVal = dictVariable.pop(i)
    dictVariable[i + 4] = newVal
# Test it out and see what it does.
# Change the number to 1 and see what happens.
# This example illustrates some of the reasons
# why doing this in loops is... bad.

Tuples:

Tuples are the most efficient groupings of items in Python, at least where performance is concerned. This is because tuples can't be changed once they're created.

Tuples can be either keys or values in dictionaries, and Python's built-in functions and loop logic allows for powerful searches using tuples.

Creating Tuples

You can declare an empty tuple like this (although, why would you want to?):

tupleVariable = tuple()

Or you can put stuff in the tuple at the beginning like this:

tupleVariable = tuple("1234")
# Will create put ('1', '2', '3', '4') inside of tupleVariable

Or like this:

tupleVariable = (1,2,3,4)
# Does the same thing as the previous example,
# except now they're integers!

Modifying Tuples

You can't add more stuff to a tuple.

You can't remove stuff from a tuple.

You can convert a tuple into a list:

tupleVariable = (1,2,3,4)
listVariable = list(tupleVariable)
# Result inside listVariable will be [1, 2, 3, 4]

And you can convert a list into a tuple:

listVariable = [1,2,3,4]
tupleVariable = tuple(listVariable)
# Result inside tupleVariable will be (1, 2, 3, 4)

You can find items using their index like this:

tupleVariable = (1,2,3,4)
item = tupleVariable[0] # This is how you do it
# Result inside item will be: 1

You can find a range of items using their indexes like this:

tupleVariable = (1,2,3,4)
item = tupleVariable[0:2] # This is how you do it
# Result inside item will be a new tuple: (1, 2)
# Note that index 0 is the starting point,
# and index 2 is not included in the result

You can't change values even if you know their index.

Tuples and Loops

You can use loops to iterate through tuples:

for i in tupleVariable:
    print(i)
# Will print each item in tupleVariable on its own line

You can't change items inside of tuples by iterating through loops.

Special Tuple Logic

Tuples can be compared, and will always compare the first item in each tuple against each other before moving on to other items:

firstTuple = (0, 9999, 777)
secondTuple = (10, 0, 4)
if firstTuple > secondTuple :
    print("First is bigger")
else :
    print("Second is bigger")
# Result will print "Second is bigger" because 10 > 0

This also works with Strings:

firstTuple = ('Aardvark', 'Zebra', 'Xenophobe')
secondTuple = ('Benjamin', 'Mary', 'Tuna')
if firstTuple > secondTuple :
    print("First is bigger")
else :
    print("Second is bigger")
# Result will print "Second is bigger" because B > A