<jupyter_start><jupyter_text>ListsSequential, Ordered Collection Creating lists<jupyter_code>x = [4,2,6,3] #Create a list with values
y = list() # Create an empty list
y = [] #Create an empty list
print(x)
print(y)<jupyter_output>[4, 2, 6, 3]
[]<jupyter_text>Adding items to a list<jupyter_code>x=list()
print(x)
x.append('One') #Adds 'One' to the back of the empty list
print(x)
x.append('Two') #Adds 'Two' to the back of the list ['One']
print(x)
x.insert(0,'Half') #Inserts 'Half' at location 0. Items will shift to make roomw
print(x)
x=list()
x.extend([1,2,3]) #Unpacks the list and adds each item to the back of the list
print(x)<jupyter_output>[1, 2, 3]<jupyter_text>Indexing and slicing<jupyter_code>x=[1,7,2,5,3,5,67,32]
print(len(x))
print(x[3])
print(x[2:5])
print(x[-1])
print(x[::-1])<jupyter_output>8
5
[2, 5, 3]
32
[32, 67, 5, 3, 5, 2, 7, 1]<jupyter_text>Removing items from a list<jupyter_code>x=[1,7,2,5,3,5,67,32]
x.pop() #Removes the last element from a list
print(x)
x.pop(3) #Removes element at item 3 from a list
print(x)
x.remove(7) #Removes the first 7 from the list
print(x)<jupyter_output>[1, 7, 2, 5, 3, 5, 67]
[1, 7, 2, 3, 5, 67]
[1, 2, 3, 5, 67]<jupyter_text>Anything you want to remove must be in the list or the location must be inside the list<jupyter_code>x.remove(20)<jupyter_output><empty_output><jupyter_text>Mutablility of lists<jupyter_code>y=['a','b']
x = [1,y,3]
print(x)
print(y)
y[1] = 4
print(y)
print(x)
x="Hello"
print(x,id(x))
x+=" You!"
print(x,id(x)) #x is not the same object it was
y=["Hello"]
print(y,id(y))
y+=["You!"] 
print(y,id(y)) #y is still the same object. Lists are mutable. Strings are immutable

def eggs(item,total=0):
    total+=item
    return total


def spam(elem,some_list=[]):
    some_list.append(elem)
    return some_list

print(eggs(1))
print(eggs(2))

print(spam(1))
print(spam(2))<jupyter_output>1
2
[1]
[1, 2]<jupyter_text>Iteration Range iteration<jupyter_code>#The for loop creates a new variable (e.g., index below)
#range(len(x)) generates values from 0 to len(x) 
x=[1,7,2,5,3,5,67,32]
for index in range(len(x)):
    print(x[index])
list(range(len(x)))<jupyter_output><empty_output><jupyter_text>List element iteration<jupyter_code>x=[1,7,2,5,3,5,67,32]
#The for draws elements - sequentially - from the list x and uses the variable "element" to store values
for element in x: 
    print(element)<jupyter_output>1
7
2
5
3
5
67
32<jupyter_text>Practice problem Write a function search_list that searches a list of tuple pairs and returns the value associated with the first element of the pair<jupyter_code>def search_list(list_of_tuples,value):
    #Write the function here
    for t in prices:
        if t[0] == value:
            return t[1]
prices = [('AAPL',96.43),('IONS',39.28),('GS',159.53)]
ticker = 'IONS'
print(search_list(prices,ticker))<jupyter_output>39.28<jupyter_text>Dictionaries<jupyter_code>mktcaps = {'AAPL':538.7,'GOOG':68.7,'IONS':4.6}

mktcaps['AAPL'] #Returns the value associated with the key "AAPL"
mktcaps['GS'] #Error because GS is not in mktcaps
mktcaps.get('GS') #Returns None because GS is not in mktcaps
mktcaps['GS'] = 88.65 #Adds GS to the dictionary
print(mktcaps) 
del(mktcaps['GOOG']) #Removes GOOG from mktcaps
print(mktcaps)
mktcaps.keys() #Returns all the keys
mktcaps.values() #Returns all the values
list1 =  [1, 2, 3, 4, 5, 6, 7]
list1[0]
list1[:2]
list1[:-2]
list1[3:5]
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(data[1][0][0])
numbers = [1, 2, 3, 4] 
numbers.append([5, 6, 7, 8])
print(len(numbers))
list1 = [1, 2, 3, 4, 5, 6, 7]

print(list1[0])
print(list1[:2])
print(list1[:-2])
print(list1[3:5])
dict1 = {"john":40, "peter":45} 
dict2 = {"john":466, "peter":45}
dict1 > dict2
dict1 = {"a":1, "b":2}# to delete the entry for "a":1, use ________.
#d.delete("a":1)
#dict1.delete("a")
#del dict1("a":1)
del dict1["a"]
dict1
s = {1, 2, 4, 3}# which of the following will result in an exception (error)? Multiple options may be correct.

#print(s[3])
print(max(s))
print(len(s))
#s[3] = 45<jupyter_output>4
4
{'fraction_non_alphanumeric': 0.1745049504950495, 'fraction_numerical': 0.061386138613861385, 'mean_word_length': 5.712624584717608, 'pattern_counts': {'":': 9, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '679', 'license': 'MIT', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1713, 'n_words': 474, 'path': 'Week 2 - A Crash Course In Python Part 2/Collections.ipynb', 'repo_name': '2series/Analytics-And-Python', 'size': '4040'}