<jupyter_start><jupyter_text>Lists  A list stores many values in a single structure. * Doing calculations with a hundred variables called `pressure_001`, `pressure_002`, etc., would be at least as slow as doing them by hand.* Use a *list* to store many values together.    * Contained within square brackets `[...]`.    * Values separated by commas `,`.* Use `len` to find out how many values are in a list.<jupyter_code>pressures = [0.273, 0.275, 0.277, 0.275, 0.276]
print('pressures:', pressures)
print('length:', len(pressures))<jupyter_output>pressures: [0.273, 0.275, 0.277, 0.275, 0.276]
length: 5<jupyter_text>Use an item's index to fetch it from a list. * Just like strings.<jupyter_code>print('zeroth item of pressures:', pressures[0])
print('fourth item of pressures:', pressures[4])<jupyter_output>zeroth item of pressures: 0.273
fourth item of pressures: 0.276<jupyter_text>Lists' values can be replaced by assigning to them. * Use an index expression on the left of assignment to replace a value.<jupyter_code>pressures[0] = 0.265
print('pressures is now:', pressures)<jupyter_output>pressures is now: [0.265, 0.275, 0.277, 0.275, 0.276]<jupyter_text>Appending items to a list lengthens it. * Use `list_name.append` to add items to the end of a list.<jupyter_code>primes = [2, 3, 5]
print('primes is initially:', primes)
primes.append(7)
primes.append(9)
print('primes has become:', primes)<jupyter_output>primes is initially: [2, 3, 5]
primes has become: [2, 3, 5, 7, 9]<jupyter_text>* `append` is a method of lists.    * Like a function, but tied to a particular object.* Use `object_name.method_name` to call methods.    * Deliberately resembles the way we refer to things in a library.* We will meet other methods of lists as we go along.    * Use `help(list)` for a preview.* `extend` is similar to `append`, but it allows you to combine two lists. For example:<jupyter_code>teen_primes = [11, 13, 17, 19]
middle_aged_primes = [37, 41, 43, 47]
print('primes is currently:', primes)
primes.extend(teen_primes)
print('primes has now become:', primes)
primes.append(middle_aged_primes)
print('primes has finally become:', primes)<jupyter_output>primes is currently: [2, 3, 5, 7, 9]
primes has now become: [2, 3, 5, 7, 9, 11, 13, 17, 19]
primes has finally become: [2, 3, 5, 7, 9, 11, 13, 17, 19, [37, 41, 43, 47]]<jupyter_text>Note that while `extend` maintains the "flat" structure of the list, appending a list to a list makes the result two-dimensional.  Use `del` to remove items from a list entirely. * `del list_name[index]` removes an item from a list and shortens the list.* Not a function or a method, but a statement in the language.<jupyter_code>print('primes before removing last item:', primes)
del primes[4]
print('primes after removing last item:', primes)<jupyter_output>primes before removing last item: [2, 3, 5, 7, 9, 11, 13, 17, 19, [37, 41, 43, 47]]
primes after removing last item: [2, 3, 5, 7, 11, 13, 17, 19, [37, 41, 43, 47]]<jupyter_text>The empty list contains no values. * Use `[]` on its own to represent a list that doesn't contain any values.    * "The zero of lists."* Helpful as a starting point for collecting values (which we will see in the next episode).  Lists may contain values of different types. * A single list may contain numbers, strings, and anything else.<jupyter_code>goals = [1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']<jupyter_output><empty_output><jupyter_text>Character strings can be indexed like lists. * Get single characters from a character string using indexes in square brackets.<jupyter_code>element = 'carbon'
print('zeroth character:', element[0])
print('third character:', element[3])<jupyter_output>zeroth character: c
third character: b<jupyter_text>Character strings are immutable. * Cannot change the characters in a string after it has been created.    * *Immutable*: can't be changed after creation.    * In contrast, lists are mutable: they can be modified in place.* Python considers the string to be a single value with parts, not a collection of values.<jupyter_code>element[0] = 'C'<jupyter_output><empty_output><jupyter_text>* Lists and character strings are both collections.  Indexing beyond the end of the collection is an error. * Python reports an `IndexError` if we attempt to access a value that doesn't exist.    * This is a kind of runtime error.    * Cannot be detected as the code is parsed because the index might be calculated based on data.<jupyter_code>print('99th element of element is:', element[99])<jupyter_output><empty_output><jupyter_text>Questions  Q1: Fill in the Blanks Fill in the blanks so that the program below produces the output shown.<jupyter_code>values = ____
values.____(1)
values.____(3)
values.____(5)
print('first time:', values)
values = values[____]
print('second time:', values)
first time: [1, 3, 5]
second time: [3, 5]<jupyter_output><empty_output><jupyter_text>**Solution**Click on the '...' below to show the solution.<jupyter_code>values = []
values.append(1)
values.append(3)
values.append(5)
print('first time:', values)
values = values[1:]
print('second time:', values)<jupyter_output><empty_output><jupyter_text>Q2: How Large is the Slice? If ‘low’ and ‘high’ are both non-negative integers, how long is the list values[low:high]? **Solution**Click on the '...' below to show the solution.<jupyter_code># The list `values[low:high]` has `high - low` elements.  For example,
# `values[1:4]` has the 3 elements `values[1]`, `values[2]`, and `values[3]`.
# Note that the expression will only work if `high` is less than the total 
# length of the list `values`.<jupyter_output><empty_output><jupyter_text>Q3: From Strings to Lists and Back Given this:<jupyter_code>print('string to list:', list('tin'))
print('list to string:', ''.join(['g', 'o', 'l', 'd']))
['t', 'i', 'n']
'gold'<jupyter_output><empty_output><jupyter_text>1. Explain in simple terms what `list('some string')` does.2. What does `'-'.join(['x', 'y'])` generate? **Solution**Click on the '...' below to show the solution.<jupyter_code>list('some string') # "splits" a string into a list of its characters. `x-y`<jupyter_output><empty_output><jupyter_text>Q4: Working With the End What does the following program print?<jupyter_code>element = 'helium'
print(element[-1])<jupyter_output><empty_output><jupyter_text>1. How does Python interpret a negative index?2. If a list or string has N elements, what is the most negative index that can safely be used with it, and what location does that index represent?3. If `values` is a list, what does `del values[-1]` do?4. How can you display all elements but the last one without changing `values`? (Hint: you will need to combine slicing and negative indexing.) **Solution**Click on the '...' below to show the solution.<jupyter_code># The program prints `m`.
# 1. Python interprets a negative index as starting from the end (as opposed to
#    starting from the beginning).  The last element is `-1`.
# 2. The last index that can safely be used with a list of N elements is element 
#    `-N`, which represents the first element.
# 3. `del values[-1]` removes the last element from the list.
# 4. `values[:-1]`<jupyter_output><empty_output><jupyter_text>Q5: Stepping Through a List What does the following program print?<jupyter_code>element = 'fluorine'
print(element[::2])
print(element[::-1])<jupyter_output><empty_output><jupyter_text>1. If we write a slice as low:high:stride, what does stride do?2. What expression would select all of the even-numbered items from a collection? **Solution**Click on the '...' below to show the solution.<jupyter_code># The program prints:
#
# `furn`
# 
# `eniroulf`
#
# 1. `stride` is the step size of the slice
# 2. The slice `1::2` selects all even-numbered items from a collection: it starts
#    with element `1` (which is the second element, since indexing starts at `0`),
#    goes on until the end (since no `end` is given), and uses a step size of `2`
#    (i.e., selects every second element).<jupyter_output><empty_output><jupyter_text>Q6: Slice Bounds What does the following program print?<jupyter_code>element = 'lithium'
print(element[0:20])
print(element[-1:3])<jupyter_output><empty_output><jupyter_text>Q7: Sort and Sorted What do these two programs print? In simple terms, explain the difference between `sorted(letters)` and `letters.sort()`. Program A<jupyter_code>letters = list('gold')
result = sorted(letters)
print('letters is', letters, 'and result is', result)<jupyter_output><empty_output><jupyter_text>Program B<jupyter_code>letters = list('gold')
result = letters.sort()
print('letters is', letters, 'and result is', result)<jupyter_output><empty_output><jupyter_text>**Solution**Click on the '...' below to show the solution.<jupyter_code># Program A -----------------------------------

# letters is ['g', 'o', 'l', 'd'] and the result is ['d', 'g', 'l', 'o']

# Program B -----------------------------------
# letters is ['d', 'g', 'l', 'o'] and the result is None

# `sorted(letters)` returns a sorted copy of the list `letters` (the original
# list `letters` remains unchanged), while `letters.sort()` sorts the list
# `letters` in-place and does not return anything.<jupyter_output><empty_output><jupyter_text>Q8: Copying (or Not) What do these two programs print? In simple terms, explain the difference between `new = old` and `new = old[:]`. Program A<jupyter_code>old = list('gold')
new = old      # simple assignment
new[0] = 'D'
print('new is', new, 'and old is', old)<jupyter_output><empty_output><jupyter_text>Program B<jupyter_code>old = list('gold')
new = old[:]   # assigning a slice
new[0] = 'D'
print('new is', new, 'and old is', old)<jupyter_output><empty_output><jupyter_text>**Solution**Click on the '...' below to show the solution.<jupyter_code># Program A ---------------------------------
#
# new is ['D', 'o', 'l', 'd'] and old is ['D', 'o', 'l', 'd']
#
# Program B ---------------------------------
# 
# new is ['D', 'o', 'l', 'd'] and old is ['g', 'o', 'l', 'd']
# 
# `new = old` makes `new` a reference to the list `old`; `new` and `old` point
# towards the same object.
#
# `new = old[:]` however creates a new list object `new` containing all elements
# from the list `old`; `new` and `old` are different objects.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1417360976554931, 'fraction_numerical': 0.025285797326099594, 'mean_word_length': 4.878701594533029, 'pattern_counts': {'":': 0, '<': 98, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '26', 'dir': 'jupyter-structured-clean-dedup', 'id': '408683', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 3535, 'n_tokens_neox': 3278, 'n_words': 1288, 'path': '_episodes_rapids/090_lists.ipynb', 'repo_name': 'UFResearchComputing/py4ai', 'size': '10322'}