<jupyter_start><jupyter_text>[05 - Data Structures](https://docs.python.org/3.5/tutorial/datastructures.html)- [Lists](5.1.-Lists) - [A,B]- [List Comprehensions](5.1.3.-List-Comprehensions)- [Nested List Comprehensions](5.1.4.-Nested-List-Comprehensions)- [The del statement](5.2.-The-del-statement)- [Tuples and Sequences](5.3.-Tuples-and-Sequences) - A,B- [Sets](5.4.-Sets) - {A,B}- [Dictionaries](5.5.-Dictionaries) - {A:value,B:value}- [Looping Techniques](5.6.-Looping-Techniques)- [Comparing Sequences and Other Types](5.8.-Comparing-Sequences-and-Other-Types)  Flatten list of lists<jupyter_code>list_of_lists = [range(4), range(7)]
flattened_list = []

for x in list_of_lists:
    for y in x:
        flattened_list.append(y)
    
flattened_list
list_of_lists = [range(4), range(7)]
[y for x in list_of_lists for y in x]<jupyter_output><empty_output><jupyter_text>5.1. Lists<jupyter_code>fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print("count of 'apple' =", fruits.count('apple'))
print("count of 'tangerine' =", fruits.count('tangerine'))
print("index of 'banana' =", fruits.index('banana'))
print("index of 'banana' from 4 =", fruits.index('banana', 4))

fruits.reverse()
print("reversed:", fruits)

fruits.append('grape')
print("appended:",fruits)

fruits.sort()
print("sorted:  ", fruits)

print("pop operation:", fruits.pop())
from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")           # Terry arrives
queue.append("Graham")          # Graham arrives
queue.popleft()                 # The first to arrive now leaves
queue.popleft()                 # The second to arrive now leaves
queue                           # Remaining queue in order of arrival<jupyter_output><empty_output><jupyter_text>5.1.3. List Comprehensions<jupyter_code>squares = []

for x in range(10):
    squares.append(x**2)

print(squares)      
print(x)
list(map(lambda x: x**2, range(10)))
# Get documentation
?map
list(enumerate([x**2 for x in range(10)]))
[(x, y) for x in range(1,4) for y in [3,1,4] if x != y]
?range
freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
[weapon.strip() for weapon in freshfruit]
matrix = [[1,2,3], [4,5,6], [7,8,9]]
[number for vector in matrix for number in vector]
from math import pi
[str(round(pi, i)) for i in range(1, 6)]<jupyter_output><empty_output><jupyter_text>5.1.4. Nested List Comprehensions<jupyter_code>matrix = [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
]
[[row[index] for row in matrix] for index in range(4)]
[row for row in matrix]
list(zip(matrix))
list(zip(*matrix))
#zip(iter1 [,iter2 [...]])
#zip(matrix[0], matrix[1], matrix[2], matrix[3])
list(zip(matrix[0], matrix[1], matrix[2]))
?zip
list(range(3, 6))            # normal call with separate arguments

args = [3, 6]
list(range(*args))            # call with arguments unpacked from a list<jupyter_output><empty_output><jupyter_text>5.2. The del statement<jupyter_code>a = [-1, 1, 66.25, 333, 333, 1234.5]
del a[0]
a
del a[2:4]
a
del a[:]
a
del a<jupyter_output><empty_output><jupyter_text>5.3. Tuples and Sequences<jupyter_code>t = 12345, 54321, 'hello!'
print(t[0])
print(t)
# immutable
t[0] = 88888
# but they can contain mutable objects:
v = ([1, 2, 3], [3, 2, 1])
v[0][1] = 4
v
# may be nested:
u = t, (1, 2, 3, 4, 5)
u
empty = ()
len(empty)
singleton = 'hello',    # <-- note trailing comma
len(singleton)
# unpacking
t = 12345, 54321, 'hello!'
x, y, z = t<jupyter_output><empty_output><jupyter_text>5.4. Sets<jupyter_code>basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}

print("duplicates have been removed:", basket)
print("fast membership testing:", 'orange' in basket, 'crabgrass' in basket)
# Demonstrate set operations on unique letters from two words
a = set('abracadabra')
b = set('alacazam')

print("unique letters in a\n", a)
print("letters in a but not in b\n", a - b)
print("letters in a or b or both\n", a | b)
print("letters in both a and b\n", a & b)
print("letters in a or b but not both\n", a ^ b)
{x for x in 'abracadabra' if x not in 'abc'}<jupyter_output><empty_output><jupyter_text>5.5. Dictionaries<jupyter_code>a = {}
a['test']
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
tel
tel['jack']
del tel['sape']
tel['irv'] = 4127
tel
list(tel)
sorted(tel)
'guido' in tel
'jack' not in tel
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{x: x**2 for x in (2, 4, 6)}
dict(sape=4139, guido=4127, jack=4098)<jupyter_output><empty_output><jupyter_text>5.6. Looping Techniques<jupyter_code>knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
    print(k, v)
for i, v in enumerate(['tic', 'tac', 'toe']):
    print(i, v)
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
    print('What is your {0}?  It is {1}.'.format(q, a))
for i in reversed(range(1, 10, 2)):
    print(i)
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
for f in sorted(set(basket)):
    print(f)
import math
raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
filtered_data = []
for value in raw_data:
    if not math.isnan(value):
        filtered_data.append(value)

filtered_data
[value for value in raw_data if not math.isnan(value)]
a, b, c = 1, 2, 2
a < b == c
string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
non_null = string1 or string2 or string3
non_null<jupyter_output><empty_output><jupyter_text>5.8. Comparing Sequences and Other Types<jupyter_code>(1, 2, 3)              < (1, 2, 4)
[1, 2, 3]              < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4)           < (1, 2, 4)
(1, 2)                 < (1, 2, -1)
(1, 2, 3)             == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18813066529844363, 'fraction_numerical': 0.04737472207969899, 'mean_word_length': 4.321201091901729, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 41, 'https://': 1, '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': '323990', 'license': 'MIT', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2293, 'n_words': 625, 'path': 'Tutorial/05 - Data Structures and Comprehensions.ipynb', 'repo_name': 'FallenGameR/LearningPython', 'size': '5847'}