Data Structures

Tuples and Lists are data structures that hold several values. These structures are important because it allows programmers the ability to store, move, and do operations on many pieces of data at once.

Here we go over four main data sturctures used in Python: Lists, Tuples, Sets, and Dictionaries. As a novice programmer, most, if not all, of the tasks you would like to accomplish using a data sturcture can be done purely using lists as a data structure. However, we briefly go over tuples, sets, and dictionaries in order to help familiarize yourself with them in case they are needed or if you have to read another programmer's code that uses these structures.

Much of this information comes from and can be found more in depth on the official python documentation.

Lists
From the official Python documentation :

''Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.''

Therefore, a list can contain any of the variable types you learned about earlier: a = [10, "John Smith", 5.0, True] Lists are mutable. This means that you can access any piece of data in the list and change it.

Lists provide an index syntax to allow you to both access and change the data stored within it. These indices are 0-indexed. This means that to access the first element, you must access it at position 0, then add one for each element afterwards.

There are several ways to access and change an array (using the array above):

Standard array access syntax: a[0] #returns 10 a[2] #returns 5.0 a[-1] #returns True. This syntax means "return the first from the end" List slicing: a[1:] #Returns ["John Smith", 5.0, True] i.e. all elements from index 1 and after a[:2] #Returns [10, "John Smith", 5.0] i.e. all elements from index 2 and before a[1:2] #Returns ["John Smith", 5.0] i.e. all elements from index 1 to index 2 a[:-2] #Returns [10, "John Smith"] i.e. all elements before the last two Concatenating two lists: a = [1, 2, 3] b = ["James", "Joan"] c = a + b # c = [1, 2, 3, "James", "Joan"] Built-in Functions:

Like other variables, lists also utilize built-in functions. For example, the len function, which can be used to calculate the length of a string, also returns the item count of a list when a list is passed to it: my_list = [5, 8, 3, 3, 5] len(my_list) #returns 5 del my_list[0] There are also functions that you call directly on a list in order to manipulate it: my_list = [1, 3, 5] list.append("Another Item") #Changes my_list to [1, 3, 5, "Another Item"] For more on functions you can perform on a list, refer to the official Python documentation on lists.
 * 1) Deletes the first value in the array
 * 1) my_list now equals [8, 3, 3, 5]

Tuples
The main use of tuples is for passing a group of ordered data around between functions (which you'll see later) and variables. A tuple is assigned using parentheses: an_empty_tuple = person_data = ("Johnny", 23, "24 Address Street") An exception is for tuples of size 1, which are assigned using a comma after the variable small_tuple = "John", Here's how you would extract variables from a tuple: person_data = ("Johnny", 23, "24 Address Street") name, age, address = person_data print address # Prints 24 Address Street Many of the functions that work on a list also work on a tuple, such as len, concatenation using the + sign, accessing by index, and slicing.
 * 1) an empty tuple

However, the main difference of a tuple that sets it apart from a list is that it is immutable. Therefore, you can not delete or add to the tuple after it has been assigned: my_tuple = (1, "Jack", 42) my_tuple.append(5) # Not allowed! del my_tuple[1] # Not allowed either! a = my_tuple[1] # This is ok because you are not changing my_tuple itself You can always reassign the my_tuple variable and use concatenation to achieve the result. However, if you need to add to a tuple then you should probably just be using a list instead!

Sets
The main purpose of a set in Python is to maintain an unordered list of unique data. Sets are also more mathematical in nature, supporting operations such as union, intersection, and difference.

Sets, like tuples, are immutable. However, because sets are unordered, you can not use operations like accessing a value by index or slicing.

Here's the example from the official Python documentation:

>>> basket = ['apple', 'orange' , 'apple' , 'pear' , 'orange' , 'banana' ] >>> fruit = set (basket)              # create a set without duplicates >>> fruit <span class="go" style="color: rgb(48, 48, 48); ">set(['orange', 'pear', 'apple', 'banana']) <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> <span class="s" style="color: rgb(64, 112, 160); ">'orange' <span class="ow" style="color: rgb(0, 112, 32); font-weight: bold; ">in fruit                <span class="c" style="color: rgb(64, 128, 144); font-style: italic; "># fast membership testing <span class="go" style="color: rgb(48, 48, 48); ">True <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> <span class="s" style="color: rgb(64, 112, 160); ">'crabgrass' <span class="ow" style="color: rgb(0, 112, 32); font-weight: bold; ">in fruit <span class="go" style="color: rgb(48, 48, 48); ">False <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> <span class="c" style="color: rgb(64, 128, 144); font-style: italic; "># Demonstrate set operations on unique letters from two words <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">... <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> a <span class="o" style="color: rgb(102, 102, 102); ">= <span class="nb" style="color: rgb(0, 112, 32); ">set (<span class="s" style="color: rgb(64, 112, 160); ">'abracadabra' ) <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> b <span class="o" style="color: rgb(102, 102, 102); ">= <span class="nb" style="color: rgb(0, 112, 32); ">set (<span class="s" style="color: rgb(64, 112, 160); ">'alacazam' ) <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> a                                 <span class="c" style="color: rgb(64, 128, 144); font-style: italic; "># unique letters in a  <span class="go" style="color: rgb(48, 48, 48); ">set(['a', 'r', 'b', 'c', 'd']) <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> a <span class="o" style="color: rgb(102, 102, 102); ">- b                             <span class="c" style="color: rgb(64, 128, 144); font-style: italic; "># letters in a but not in b  <span class="go" style="color: rgb(48, 48, 48); ">set(['r', 'd', 'b']) <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> a <span class="o" style="color: rgb(102, 102, 102); ">| b                             <span class="c" style="color: rgb(64, 128, 144); font-style: italic; "># letters in either a or b  <span class="go" style="color: rgb(48, 48, 48); ">set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> a <span class="o" style="color: rgb(102, 102, 102); ">& b                             <span class="c" style="color: rgb(64, 128, 144); font-style: italic; "># letters in both a and b  <span class="go" style="color: rgb(48, 48, 48); ">set(['a', 'c']) <span class="gp" style="color: rgb(198, 93, 9); font-weight: bold; ">>>> a <span class="o" style="color: rgb(102, 102, 102); ">^ b                             <span class="c" style="color: rgb(64, 128, 144); font-style: italic; "># letters in a or b but not both <span class="go" style="color: rgb(48, 48, 48); ">set(['r', 'd', 'b', 'm', 'z', 'l'])

Dictionaries
Dictionaries, also known in other programming languages as hashes or associative arrays, are extremely useful for keeping track of key-value pairs. Instead of accessing a value through an index, like in a list and tuple, dictionaries use a key. This is an extremely powerful tool that is very common in practical computer applications.

To initialize a dictionary, the syntax is similar to a list, except it curly braces -- {} -- are used to surround key-value pairs denoted by key:value For example: person_data = { "name": "Johhny", "age": 23, "address": "24 Address Street" } Now, what a programmer is accessing in a dictionary is clear and readable when the code is reused: person_data["name"] By quickly glancing over the code, you can easily see that the name of the person is being accessed as opposed to code that looks like: person_data[0] where you know that it is a person's data, but what part of it?

Adding to a dictionary is easy and also helps make code readable: person_data["gender"] = "male" Even if that key was never used before in the dictionary, it is now initialized. If there was data already in the dictionary using the key "gender" then it is overwritten by your new value.