<jupyter_start><jupyter_text>Python lists* list    * unpacking    * indexing    * slicing    * nested lists    * edit, append, insert, del* tuple* for loop* reference vs copy* dictionary  List<jupyter_code>mylist = [1, 2, 3.4, "hi", True]

mylist
# create an empty list
alist = []

alist<jupyter_output><empty_output><jupyter_text>Unpacking lists<jupyter_code>mylist = [1, 2, "cat"]

(a, b, c) = mylist

print(a)
print(b)
print(c)<jupyter_output>1
2
cat<jupyter_text>List indexing![](images/list-indexing.png)  List indexing<jupyter_code>mylist = [1, 2, 3.4, "hi", True]

mylist[0]
mylist[1]
mylist[-1], mylist[-2]
i = 3
mylist[i]<jupyter_output><empty_output><jupyter_text>Quiz 1 ~2:10 pm```pythona = [4, 2, 7, 1, 9]```How would you refer to the value 7 in the above list?*On your honor, please do not test this in a code cell.*    A) a[3]    B) a[4]    C) a[2]    D) a[-2]  List slicing```pythonlist[start:stop:step]```![](images/list-slice-start-stop.png)  List slicing```pythonlist[start:stop:step]```<jupyter_code>mylist = [1, 2, 3.4, "hi", True, 5, 82, 99]

mylist[1:4]
mylist[1:7:2]<jupyter_output><empty_output><jupyter_text>List slicing defaults```pythonlist[start:stop:step]```<jupyter_code>mylist = [1, 2, 3.4, "hi", True, 5, 82, 99]

mylist[4:]
mylist[:4]
mylist[:]
mylist[::2]<jupyter_output><empty_output><jupyter_text>Quiz 2 ~2:20 pm```pythona = [4, 2, 7, 1, 9]```How would you refer to the middle three values in the above list?*On your honor, please do not test this in a code cell.*    A) a[2:-2]    B) a[1:-1]    C) a[2:4]    D) a[1:4]  Take a minute to collect your thoughts...  Is the presentation clear?![](../../images/zoom-yes-no.png)  Do you want me to go slower or faster?![](../../images/zoom-slower-faster.png)  Nested lists<jupyter_code>mylist = [1, 2, [3, 4]]

mylist[2]
mylist[2][0]<jupyter_output><empty_output><jupyter_text>Can create multi-dimensional arrays, but these are not very nice to work with  Editing lists<jupyter_code>mylist = [1, 2, 3.4, "hi", True, 8]

mylist[1] = 5
mylist[-2] = False
mylist[2] = mylist[2] * 10
mylist[-1] *= 10

mylist<jupyter_output><empty_output><jupyter_text>Append items<jupyter_code>mylist = [1, 2]

mylist.append(3)

mylist
mylist.append([4, 5])

mylist
mylist.extend([4, 5])

mylist<jupyter_output><empty_output><jupyter_text>Insert items<jupyter_code>mylist = [1, 2, 3]

mylist.insert(1, 1.5)

mylist
mylist.insert(0, 0.5)

mylist<jupyter_output><empty_output><jupyter_text>Removing items<jupyter_code>mylist = [1, 2, 3, [True, False], 4, 5]

del(mylist[2])

mylist
del(mylist[2])

mylist
del(mylist[::2])

mylist<jupyter_output><empty_output><jupyter_text>Quiz 3 ~2:35 pm```pythona = [-1, 7, 5]a.append(-1)a.insert(1, 3)del(a[-1])```What is **a**?*On your honor, please do not run the above code.*    A) [3, 7, 5, -1]    B) [-1, 3, 7, 5]    C) [3, -1, 7, 5]    D) [-1, 3, 7, -1]  Tuples<jupyter_code>mytuple = (1, "hi", True)

mytuple
mytuple = (1, "hi", True, [5, 6], ('a', 7))

mytuple
mylist = list(mytuple)

mylist<jupyter_output><empty_output><jupyter_text>Tuples - immutable lists<jupyter_code># can edit list items
mylist[0] = 10

mylist
# cannot edit tuple items
mytuple[0] = 10<jupyter_output><empty_output><jupyter_text>Time check ~2:40 pm  Take a minute to collect your thoughts...  Is the presentation clear?![](../../images/zoom-yes-no.png)  Do you want me to go slower or faster?![](../../images/zoom-slower-faster.png)  Iterating over lists<jupyter_code>mylist = [1, 2, 3.4, "hi", True, [5, 6]]

for item in mylist:
    print(item)
mylist = [1, 2, 3.4, "hi", True, [5, 6]]

len(mylist) # length of mylist
for i in range(len(mylist)):
    print(i, mylist[i])
for (i, val) in enumerate(mylist):
    print(i, val)<jupyter_output>0 1
1 2
2 3.4
3 hi
4 True
5 [5, 6]<jupyter_text>Editing lists in for loops<jupyter_code>mylist = [2, 7, 9]

for number in mylist:
    number = number * 10
    print(number)

mylist
for (i, number) in enumerate(mylist):
    mylist[i] = number * 10

mylist<jupyter_output><empty_output><jupyter_text>Quiz 4 ~2:55 pm```pythona = [3, 6, 9, 12, 15]b = []for val in a:    if val >= 9:        b.append(val)```What is **b**?*On your honor, please do not run the above code.*    A) []    B) [3, 6, 9, 12, 15]    C) [12, 15]    D) [9, 12, 15]  loop skip and exit<jupyter_code>for i in range(10):
    if i == 3:
        continue  # skip to next loop iteration
    if i == 7:
        break  # exit the loop
    print(i)

print("done")<jupyter_output>0
1
2
4
5
6
done<jupyter_text>Take a minute to collect your thoughts...  Is the presentation clear?![](../../images/zoom-yes-no.png)  Do you want me to go slower or faster?![](../../images/zoom-slower-faster.png)  Reference vs Copy<jupyter_code>a = 1
b = a  # b = 1
b = 2

print(a)
print(b)
a = [1, 2, 3]
b = a  # b is a reference to the same list as a
b[0] = 10

print(a)
print(b)<jupyter_output>[10, 2, 3]
[10, 2, 3]<jupyter_text>You can't reference an index or slice, only the list object itself<jupyter_code>a = [1, 2, 3]
b = a[0]
b = -1

print(a)
print(b)
b = a[1:]
b[0] = 10

print(a)
print(b)<jupyter_output>[1, 2, 3]
[10, 3]<jupyter_text>What if you want to copy a list?<jupyter_code>import copy

a = [1, 2, 3]
b = copy.copy(a)
b[0] = 10

print(a)
print(b)
a = [[1, 2], [3, 4]]
b = copy.deepcopy(a)  # <-- !!! copy won't work for nested lists
b[0][1] = 20

print(a)
print(b)<jupyter_output>[[1, 2], [3, 4]]
[[1, 20], [3, 4]]<jupyter_text>List comprehensions In my opinion, these should be AVOIDED for the sake of code readability except for in the simplest cases. If you want to learn about them, you're on your own.  DictionaryList of key/value pairs.<jupyter_code>mydict = { "id": 32, "animal": "bird"}

mydict
mydict["id"]
mydict["animal"] = "cat"

mydict
mydict["city"] = "Denver"

mydict<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17340940525587828, 'fraction_numerical': 0.04961964038727524, 'mean_word_length': 4.416666666666667, 'pattern_counts': {'":': 2, '<': 72, '<?xml version=': 0, '>': 72, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '249147', 'license': 'Unlicense', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2477, 'n_words': 676, 'path': 'lectures/Jan-21-lists/lists.ipynb', 'repo_name': 'nishadalal120/NEU-365P-385L-Spring-2021', 'size': '5784'}