<jupyter_start><jupyter_text>Python Lists Quick Reference Table Of Contents1. Construction2. Accessing Data3. Modifying4. Sorting5. Deep Copy6. Equality and Identity7. List Comprehensions  1. Construction<jupyter_code>#create an empty list
empty_list=[]
empty_list=list()
simpsons = ['homer', 'marge', 'bart']<jupyter_output><empty_output><jupyter_text>2. Accessing Data<jupyter_code>simpsons[0]
len(simpsons)
# counts the number of instances
simpsons.count('bart')
# returns index of the first reference
simpsons.index('marge')<jupyter_output><empty_output><jupyter_text>3. Modifying<jupyter_code>#append to end
simpsons.append('lisa')
simpsons
#append multiple elments to end
simpsons.extend(['itchy','scratchy'])
simpsons
#insert at index (shifts all elements to the right)
simpsons.insert(0, 'maggie')
simpsons
#remove element 0 and return it
simpsons.pop(0)
#remove element at 0 (does not return it)
del simpsons[0]
simpsons
#search for first instance and remove it
simpsons.remove('bart')
simpsons
#replace elmeent
simpsons[0]='krusty'
simpsons
#conctonate lists (slower then 'extend' method)
neighbors = simpsons + ['ned','rod','todd']
neighbors<jupyter_output><empty_output><jupyter_text>**Setwise list manipulations:**<jupyter_code>neighbors * 2<jupyter_output><empty_output><jupyter_text>4. Sorting **Sort a list in place (modifies but does not return the list):**<jupyter_code>simpsons.sort()
simpsons
#reverse sort
simpsons.sort(reverse=True)
simpsons
#sort by a key
simpsons.sort(key=len)
simpsons<jupyter_output><empty_output><jupyter_text>**Return a sorted list (does not modify the original list):**<jupyter_code>sorted(simpsons)
sorted(simpsons, reverse=True, key=len)<jupyter_output><empty_output><jupyter_text>**Insert into an already sorted list, and keep it sorted:**<jupyter_code>num = [10, 20, 40, 50]
from bisect import insort
insort(num, 30)
num<jupyter_output><empty_output><jupyter_text>5. Deep Copy<jupyter_code>new_num = num[:]
new_num=list(num)
new_num<jupyter_output><empty_output><jupyter_text>6. Equality and Identity<jupyter_code># Identity compare: are they referencing the same object?
num is new_num
# Equality compare: do they have the same data?
num == new_num<jupyter_output><empty_output><jupyter_text>7. List Comprehensions<jupyter_code>mylist = [1, 4, -5, 10, -7, 2, 3, -1]
[n for n in mylist if n > 0]
[n for n in mylist if n < 0]
[-5, -7, -1]
import math
[math.sqrt(n) for n in mylist if n > 0]
#clip negative values
[n if n > 0 else 0 for n in mylist]
# nested loops flattened into one list
alphas = 'abc'
digits = '123'
correct = ['a1', 'a2','a3','b1','b2','b3','c1','c2','c3']

answer = [alpha+digit for alpha in alphas for digit in digits]
answer == correct<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1253654970760234, 'fraction_numerical': 0.0222953216374269, 'mean_word_length': 7.293939393939394, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 44, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '552559', 'license': 'MIT', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1017, 'n_words': 276, 'path': 'Python Quick Reference/Lists.ipynb', 'repo_name': 'eriksalt/jupyter', 'size': '2736'}