<jupyter_start><jupyter_text>List Create List<jupyter_code>books = ['Wiedzmin', '<NAME>', 'Nowicjuszka']
numbers = [42, 123]
empty = []
print(books, numbers, empty)<jupyter_output>['Wiedzmin', '<NAME>', 'Nowicjuszka'] [42, 123] []<jupyter_text>Access List index 0<jupyter_code>books[0]<jupyter_output><empty_output><jupyter_text>Change elements in list<jupyter_code>print(numbers)
numbers[1] = 5
numbers
'Wiedzmin' in books
'Krótka Historia' in books
for books in books:
    print(books)
for i in range(len(numbers)):
    numbers[i] = numbers[i] * 2
    print(numbers)
#A for loop over an empty list never runs the body:
for x in []:
    print('This never happens.')
#Although a list can contain another list, the nested list still counts as a single element. 
#The length of this list is four:

list1 = ['a','b','c']
list2 = [1, 2, list1, 3]
print(list2)<jupyter_output>[1, 2, ['a', 'b', 'c'], 3]<jupyter_text>List Operations<jupyter_code>a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
c
[0] * 4
[1, 2, 3] * 3
t = ['a', 'b', 'c', 'd', 'e', 'f']
t[1:3]
t[:4]
t[3:]
t[:]
t = ['a', 'b', 'c', 'd', 'e', 'f']
t[1:3] = ['y', 'z']
t<jupyter_output><empty_output><jupyter_text>List methods<jupyter_code>t = ['a', 'b', 'c']
t.append('d')
t
t1 = ['a', 'b', 'c']
t2 = ['d', 'e']
t1.extend(t2)
t1
t = ['d', 'c', 'e', 'b', 'a']
t.sort()
t
t = [1, 2, 3]
sum(t)<jupyter_output><empty_output><jupyter_text>Map<jupyter_code>def capitalize_all(t):
    res = []
    for s in t:
        res.append(s.capitalize())
    return res<jupyter_output><empty_output><jupyter_text>Filter<jupyter_code>def only_upper(t):
    res = []
    for s in t:
        if s.isupper():
            res.append(s)
    return res
t = ['a', 'b', 'c']
x = t.pop(1)
print(t)
x

#pop modifies the list and returns the element that was removed. 
#If you don’t provide anindex, it deletes and returns the last element.
t = ['a', 'b', 'c']
del t[1]
t
t = ['a', 'b', 'c']
t.remove('b')
t
#If you know the element you want to remove (but not the index), you can use remove:
#To remove more than one element, you can use del with a slice index:
t = ['a', 'b', 'c', 'd', 'e', 'f']
del t[1:5]
t<jupyter_output><empty_output><jupyter_text>Lists and strings<jupyter_code>s = 'spam'
t = list(s)
t
s = 'pining for the fjords'
t = s.split()
t
s = 'spam-spam-spam'
delimiter = '-'
t = s.split(delimiter)
t
t = ['pining', 'for', 'the', 'fjords']
delimiter = ' '
s = delimiter.join(t)
s<jupyter_output><empty_output><jupyter_text>Tuples<jupyter_code>t = ('a', 'b', 'c', 'd', 'e')
t
t = tuple('Merlin')
t
t[0]
#you are unable to replace tuple
t[0] = 'P'
#buy you can replace it with another tuple
t = ('P',) + t[1:]
t
a = 1
b = 0 
temp = a
a = b
b = temp

# but you can use tuple assignment:
a, b = b, a<jupyter_output><empty_output><jupyter_text>you can also use tuples to split email address into a user name and domian:<jupyter_code>addr = '<EMAIL>@cdprojektred.com'
uname, domian = addr.split('@')
print(uname)
print(domian)<jupyter_output>witcher
cdprojektred.com<jupyter_text>The built-in function divmod takes two arguments and returns a tuple of two values,the quotient and remainder. You can store the result as a tuple:<jupyter_code>t = divmod(6,4)
t
pierwsza, druga = divmod(6,4)
print(pierwsza)
print(druga)<jupyter_output>1
2<jupyter_text>Lists and tuples zip is a built-in function that takes two or more sequences and returns a list of tuples whereeach tuple contains one element from each sequence. The name of the function refers to azipper, which joins and interleaves two rows of teeth.<jupyter_code>s = 'abc'
t = [0,1,2]
zip(s,t)
for pair in zip(s,t):
    print(pair)<jupyter_output>('a', 0)
('b', 1)
('c', 2)<jupyter_text>you can also use zip to create list<jupyter_code>list(zip(s, t))<jupyter_output><empty_output><jupyter_text>If the sequences are not the same length, the result has the length of the shorter one.<jupyter_code>list(zip('Anne', 'Elk'))
t = [('a', 0), ('b', 1), ('c', 2)]
for letter, number in t:
    print(number, letter)<jupyter_output>0 a
1 b
2 c<jupyter_text>Dictionary<jupyter_code>eng2pl = dict()
eng2pl['one'] = 'jeden'
eng2pl<jupyter_output><empty_output><jupyter_text>Lets enter more than one element:<jupyter_code>eng2pl = {'one':'jeden', 'two': 'dwa', 'tree': 'trzy'}
eng2pl<jupyter_output><empty_output><jupyter_text>If you print eng2pl you can see that order might not be the same<jupyter_code>eng2pl['two']
len(eng2pl)<jupyter_output><empty_output><jupyter_text>Dictonary as counter<jupyter_code>def histogram(s):
    d = dict()
    for l in s:
        if l not in d:
            d[l] = 1
        else:
            d[l] += 1
    return d
    
h = histogram('Monty Python')
h
h.get('o', 0)
h.get('a', 0)
def print_hist(h):
    for c in h:
        print(c, h[c])

        print_hist(h)<jupyter_output><empty_output><jupyter_text>To put dictionary in to alpa<jupyter_code>for key in sorted(h):
    print(key, h[key])
def invert_dict(d):
    inverse = dict()
    for key in d:
        val = d[key]
        if val not in inverse:
            inverse[val] = [key]
        else:
            inverse[val].append(key)
    return inverse

d = invert_dict(h)
d<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16359759643341734, 'fraction_numerical': 0.01744524132583834, 'mean_word_length': 4.49520766773163, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 74, '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': '317950', 'license': 'MIT', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2100, 'n_words': 607, 'path': 'python/list.ipynb', 'repo_name': 'piszewc/python-deep-learning-data-science', 'size': '5167'}