<jupyter_start><jupyter_text>04-Lists and dictionaries Python would be a fairly useless language if it weren't for the compound data types. The main two are **lists** and **dictionaries**. We have already discussed **sets** and **tuples**. As mathematicians and scientists, you'll also find **numpy arrays** useful, because they are designed to handle numeric data but that will be done later, in the third week on this class.  04.0. ListsA list is an ordered, indexable collection of data. Lets say you have collected some current and voltage data that looks like this:    voltage:        -2.0        -1.0        0.0        1.0        2.0    current:        -1.0        -0.5        0.0        0.5        1.0So you could put that data into lists like:<jupyter_code>voltage = [-2.0, -1.0, 0.0, 1.0, 2.0]

current = [-1.0, -0.5, 0.0, 0.5, 1.0]<jupyter_output><empty_output><jupyter_text>**voltage** is of type list:<jupyter_code>type(voltage)<jupyter_output><empty_output><jupyter_text>And to find the value of the third item<jupyter_code>voltage[2]<jupyter_output><empty_output><jupyter_text>Lists can be indexed from the back using a negative index. The last item of current<jupyter_code>voltage[-1]<jupyter_output><empty_output><jupyter_text>and the next-to-last<jupyter_code>current[-5]<jupyter_output><empty_output><jupyter_text>You can "slice" items from within a list. Lets say we wanted the second through fourth items from voltage.<jupyter_code>voltage[1:4]<jupyter_output><empty_output><jupyter_text>As you can see this slice returns all items for which the index is in $1 \leq i < 4$. You can leave the second space blank to get all remaining items. For example, the third item to the end can be obtained by<jupyter_code>voltage[2:]<jupyter_output><empty_output><jupyter_text>ExercisePower is defined as voltage multiplied by current. What is the power for the second entry? For the fourth? Print a list that stores the power for all the entries.<jupyter_code>for i in range (0,len(voltage))
power[i] = [voltage[i]**current[i]]
power=[voltage[i]**current[i] for i in range (len(voltage))]
power
test = 'a'
print(test == chr(97))
print(str('97'))
for i in range (0,5):
    print(voltage[i]**current[i])<jupyter_output>-0.5
(6.123233995736766e-17-1j)
1.0
1.0
2.0<jupyter_text>Append and ExtendJust like strings have methods, lists do too.<jupyter_code>dir(list)<jupyter_output><empty_output><jupyter_text>One useful method is append. Lets say we want to stick the following data on the end of both our lists :    voltage:        3.0        4.0            current:        1.5        2.0If you want to append items to the end of a list, use the append method.<jupyter_code>voltage.append(3.0)
voltage.append(4.0)
voltage<jupyter_output><empty_output><jupyter_text>You can see how that approach might be tedious in certain cases. If you want to concatenate a list onto the end of another one, use extend.<jupyter_code>current.extend([1.5, 2.0])
current<jupyter_output><empty_output><jupyter_text>Python lists can also be extended using the addition operator<jupyter_code>[3, "Yes"] + ["AIMS", 6]<jupyter_output><empty_output><jupyter_text>Heterogeneous DataLists can contain hetergeneous data.<jupyter_code>data = ["experiment: current vs. voltage", 
        "run", 47,
        "temperature", 372.756, 
        "current", [-1.0, -0.5, 0.0, 0.5, 1.0], 
        "voltage", [-2.0, -1.0, 0.0, 1.0, 2.0],
        ]
print(data)<jupyter_output>['experiment: current vs. voltage', 'run', 47, 'temperature', 372.756, 'current', [-1.0, -0.5, 0.0, 0.5, 1.0], 'voltage', [-2.0, -1.0, 0.0, 1.0, 2.0]]<jupyter_text>We've got strings, ints, floats, and even other lists in there.   ExerciseMake a list that contains the following: your first name, your age as an integer, and your last name. Then, using that list, print a single string that looks like```  is  years old.```<jupyter_code>fname = input("Enter Fname >>>")
lname = input("Enter Lname >>>")
age = input("Enter age >>>")

name = [fname, lname, age]

print(name[0]+" "+name[1]+" is "+name[2]+" years old.")<jupyter_output>Enter Fname >>>test
Enter Lname >>>best
Enter age >>>45
test best is 45 years old.<jupyter_text>Length of ListsSometimes you want to know how many items are in a list. Use the len command.<jupyter_code>len(voltage)<jupyter_output><empty_output><jupyter_text>Assigning Variables to Other VariablesSomething that might cause you headaches in the future is how python deals with assignment of one variable to another. When you set a variable equal to another, both variables point to the same thing. Changing the first one ends up changing the second. Be careful about this fact.<jupyter_code>a = [1, 2]
b = a
a.append(10)
b
The way to deal with that is to do the following:
import copy as cp

c = [1, 2, "like"]


print(d)
print(c)
c. append("let's see")
c,d<jupyter_output>[1, 2, 'like', "let's see"]
[1, 2, 'like']<jupyter_text>04.1 DictionariesA Python dictionary is a unordered collection of key-value pairs.  Dictionaries are likely the most useful data type in Python you will use in everyday programming. The key is a way to name the data, and the value is the data itself. Here's a way to create a dictionary that contains all the data in our data.dat file in a more sensible way than a list.<jupyter_code>default_data = {
            'item1': 1,
            'item2': 2,
}
default_data
default_data['item3'] = (1,2)
default_data
data = {"experiment": "current vs. voltage",
        "run": 47,
        "temperature": 372.756, 
        "current": [-1.0, -0.5, 0.0, 0.5, 1.0], 
        "voltage": [-2.0, -1.0, 0.0, 1.0, 2.0]
        }
data["test"] = "test"
data
data
print (data)<jupyter_output>{'temperature': 372.756, 'voltage': [-2.0, -1.0, 0.0, 1.0, 2.0], 'experiment': 'current vs. voltage', 'run': 47, 'current': [-1.0, -0.5, 0.0, 0.5, 1.0]}<jupyter_text>This model is clearly better because you no longer have to remember that the run number is in the second position of the list, you just refer directly to "run":<jupyter_code>data["run"]<jupyter_output><empty_output><jupyter_text>If you wanted the voltage data list:<jupyter_code>data["voltage"].append(14)
data["voltage"]
x = data["voltage"]
print (x)
data["voltage"]<jupyter_output><empty_output><jupyter_text>Or perhaps you wanted the last element of the current data list<jupyter_code>data["temperature"] = 3275.325
data["temperature"]<jupyter_output><empty_output><jupyter_text>You can also add new keys to the dictionary.  Note that dictionaries are indexed with square braces, just like lists--they look the same, even though they're very different.<jupyter_code>data["user"] = "<NAME>"
print (data)<jupyter_output>{'user': '<NAME>', 'voltage': [-2.0, -1.0, 0.0, 1.0, 2.0], 'current': [-1.0, -0.5, 0.0, 0.5, 1.0], 'temperature': 3275.325, 'run': 47, 'experiment': 'current vs. voltage'}<jupyter_text>Dictionaries, like strings, lists, and all the rest, have built-in methods. Lets say you wanted all the keys from a particular dictionary.<jupyter_code>data.keys()<jupyter_output><empty_output><jupyter_text>also, values<jupyter_code>data.values()<jupyter_output><empty_output><jupyter_text>ExerciseYou have an additional voltage and current reading of 1.5 and 3, respectively. Add this observation to the `data` dictionary and show that it has been added.<jupyter_code>voltage.append(1.5)
current.append(3)
voltage,current
data["voltage"] = voltage
data["current"] = current
voltage,current<jupyter_output><empty_output><jupyter_text>Now, print the _difference_ between the power observed in the last two readings.<jupyter_code>diff = len(voltage) - len(data["voltage"])
voltage,data["voltage"]
for diff in range (diff,0):
    print(data["voltage"][diff])
voltage,data["voltage"]
voltage
data["voltage"].append(15)
data["voltage"]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1272750576775186, 'fraction_numerical': 0.037298128684952574, 'mean_word_length': 4.724871606749817, 'pattern_counts': {'":': 6, '<': 98, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '315554', 'license': 'MIT', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2588, 'n_words': 888, 'path': '04-Lists-dictionaries.ipynb', 'repo_name': 'sustecha/AIMS_Python_course', 'size': '7822'}