<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.insert(0, "Hello")
print(x)
x=list()
x.extend([1,2,3]) #Unpacks the list and adds each item to the back of the list
print(x)
x=list()
x.append([1,2,3])
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># ValueError: list.remove(x): x not in list
# 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=[]):  # Default to [].
    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>Tuple<jupyter_code>price = ("20150904",545.23)
price[0]
price[1]
# TypeError: 'tuple' object does not support item assignment
# price[1] = 26.3
# price[2]  #IndexError: tuple index out of range<jupyter_output><empty_output><jupyter_text>Iteration Range iteration<jupyter_code>prices = [("AAPL",96.43),("IONS",39.28),("GS",159.53)]
for stock_price in prices:
    # stock_price is a variable that wil map to each element.
    print(stock_price[0], stock_price[1])
"""
prices = [("AAPL",96.43),("IONS",39.28),("GS",159.53)]
ticker = input("Please enter a ticker: ")
for item in prices:
    if item[0] == ticker:
        print(ticker,item[1])
        break
    else:
        print("Sorry".ticker."was not found in my database")
    print("Statement after for")
"""
#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)))  # it's generating a series of numbers.
list(range(15))<jupyter_output><empty_output><jupyter_text>List element iteration<jupyter_code>x=[1,7,2,5,3,5,67,32]
for element in x: #The for draws elements - sequentially - from the list x and uses the variable "element" to store values
    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 element in list_of_tuples:
        if element[0] == value:
            return element[1]
        return 0
prices = [('AAPL',96.43),('IONS',39.28),('GS',159.53)]
ticker = 'AAPL'
print(search_list(prices,ticker))<jupyter_output>96.43<jupyter_text>Dictionaries<jupyter_code># 'AAPL' is key, 538.7 is value.
mktcaps = {'AAPL':538.7,'GOOG':68.7,'IONS':4.6}
type(mktcaps)
mktcaps.keys()
mktcaps.values()
for key in mktcaps:
    print(key, mktcaps[key])
mktcaps['AAPL'] #Returns the value associated with the key "AAPL"
print(mktcaps['AAPL'] )
# mktcaps['GS'] #Error because GS is not in mktcaps
print(mktcaps) 
"GE" 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
sorted(mktcaps.keys())  # Returns a sorted list of keys<jupyter_output><empty_output><jupyter_text>Sets<jupyter_code>tickers = {'AAPL',"GE",'NFLX','IONS'}
regions = {"North East", "South", "West Coast", "Mid-West"}
type(mktcaps)
"AAPL" in tickers  # Membership test
"IBM" not in tickers  # Non-membership test
pharma_tickers = {"IONS","IMCL"}
tickers.isdisjoint(pharma_tickers)  # Empty intersection
pharma_tickers <= tickers  # Subset test
pharma_tickers < tickers # Proper-subset test
tickers > pharma_tickers  # Superset
tickers & pharma_tickers  # Intersection
tickers | pharma_tickers  # Union
tickers - pharma_tickers  # Set difference<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1549103663289166, 'fraction_numerical': 0.04345284489477787, 'mean_word_length': 5.6061776061776065, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 43, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '102622', 'license': 'MIT', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2040, 'n_words': 541, 'path': 'Collections.ipynb', 'repo_name': 'kaiicheng/Analytics-in-Python', 'size': '5132'}