<jupyter_start><jupyter_text>Python 3rd step: List and TupleList and Tuple are similar classes. Both of them can have any type of objects as elements in it.Most of the operation that can be done in Tuple can be done in List, so I will mainly focus on List hereafter.In order to make List use [] or list() function. In order to make Tuple, use tuple() function or comma + ().Python does not support simple array (without external library). List is used where array is required in other language.<jupyter_code># empty list and tuple
x = list()
print(x)

x = tuple()
print(x)
# Make a List with elements
x = [1, 2, 3]
print(x)

# List with different data type
x = ['a', 1, 'b', 2]
print(x)

# List of List
x = [[1,2,3], [4,5,6]]
print(x)<jupyter_output><empty_output><jupyter_text>Mutable and ImmutableThe difference of Tuple and List is that List is **Mutable**(changeable), but Tuple is **Immutable**(Not changeable).When copying list, care must be taken. *list_new = list_old* just refers same object, so modification to one will affect another. In order to avoid that condition, use copy() method or slice.<jupyter_code>t = (1, 2, 3)
t[1] = 5
print(t)
t = (1, [2], 3)
#print(id(t), id(t[0]), id(t[1]), id(t[2]))
#print(id(t[1][0]))
t[1][0] = 5
print(t)
#print(id(t), id(t[0]), id(t[1]), id(t[2]))
#print(id(t[1][0]))
x = [1, 2, 3]
y = x
y[0] = 4
print(x)
print(id(x), id(y))
x = [1, 2, 3]
y = x.copy()  # or y = x[:]
y[0] = 4
print(x)
print(id(x), id(y))<jupyter_output><empty_output><jupyter_text>Index and SliceLike String, Tuple and List support Index and Slice.<jupyter_code>x = [1, 2, 3, 4, 5, 6]
print(x[3])
print(x[1:4])
print(x[-1])
print(x[::-1])<jupyter_output><empty_output><jupyter_text>Membership testLike String, Tuple and List support **in** and **not in** oprator.  **in** and **not in** returns boolean value **True** or **False**.<jupyter_code>x = [1, 2, 3, 4, 5, 6]
print('3 in x:', 3 in x)
print('4 not in x:', 4 not in x)
if 7 in x:
    print('7 is in list: x')
else:
    print('7 is not in x')
x = [1, 2, [3,4], 5]
print('3 in x:', 3 in x)
print('[1,2] in x', [1,2] in x)<jupyter_output><empty_output><jupyter_text>LengthLike String, len() function returns number of elements in the List<jupyter_code>x = [1, 2, 3]
print(len(x))

for i in range(len(x)):
    if i > 0:
        print(x[i-1] + x[i])
    
# List of List
x = [[1,2,3], [4,5,6]]
print('Number of element of x:', len(x), 'Number of element of x[0]:', len(x[0]))<jupyter_output><empty_output><jupyter_text>List ComprehensionsList Comprehenstions are often used in order to initialize. Its basic format is as follows. **If** statement is optional.```list_item = [expression  for ... if ...]```<jupyter_code>x = [i for i in range(5)]
print (x)

# same result as below
y = list()
for i in range(5):
    y.append(i)
print(y)
x = [i**2 for i in range(5) if i != 3]
print(x)
def comprehension(n):
    return [i for i in range(n)]

def append_loop(n):
    x = list()
    for i in range(n):
        x.append(i)
    return x

print('List comprehension')
%timeit comprehension(1000000)
print('Append Loop')
%timeit append_loop  (1000000)<jupyter_output><empty_output><jupyter_text>Hands onCreate the list of 100 None using List comprehension<jupyter_code># Change ??? to create list of 100 None

x = [??? for ??? in ???]
print(x)<jupyter_output><empty_output><jupyter_text>Method of ListList has many built-in method. help(list) shows how to use them<jupyter_code>help(list)<jupyter_output><empty_output><jupyter_text>List manipulation Add item in Listappend() method is used to add 1 item to end of list. extend() method and *+* operator are used to concatenate 2 lists. insert() method is used to insert at specific offset(position).<jupyter_code>x = [1,2]
x.append(3)
print(x)

y = [10,11]
x += y
print(x)

x.extend(y)
print(x)

x.insert(3, 4)
print(x)<jupyter_output><empty_output><jupyter_text>Remove item from Listpop() method is used to remove itme at specific position and return its value. (default: last element)removed() method is used to removed specific value.<jupyter_code>x = [1,2,3,1,2,3]
val = x.pop()
print('x:', x, 'val:', val)

val = x.pop(0)   # pop first element
print('x:', x, 'val:', val)

x.remove(2)
print(x)<jupyter_output><empty_output><jupyter_text>Stack (LIFO)  and FIFO queueWith append() and pop() method, STACK (Last in First Out) and FIFO (First In First Out) queue can be implemented.<jupyter_code>def push(queue, val):
    queue.append(val)
    
def lifo_pop(queue):
    return(queue.pop())

def fifo_pop(queue):
    return(queue.pop(0))
x = [1,2,3]
push(x, 4)
print(fifo_pop(x))
print(lifo_pop(x))
# Sample of FIFO handling
# MCPC Problem-D Car Showroom

def set_car(car, room, room_size):
    '''
        car: String representing Car number
        room: showroom (List)
        room_size: max length of the room
        Return True if car placement is required
               False if car is already in the room
    '''
    if car in room:
        room.remove(car)
        room.append(car)
        return False

    if len(room) < room_size:
        room.append(car)
        return True

    # Remove LRU(top of list), and add new car to bottom of list
    room.pop(0)
    room.append(car)
    return True

## Main routine

showroom = list()

for car in (8, 1, 2, 3, 1, 4, 1, 5, 3, 4, 1, 4, 3, 2, 3, 1, 2, 8, 1, 2):
    print(set_car(car, showroom, 3), showroom)<jupyter_output><empty_output><jupyter_text>2-Dementional ListList can contain another List.<jupyter_code># MCPC Rehearsal Problem-A
s1 = '<NAME>'
s2 = 'BATHROOM <NAME>'

database = list()

database.append(s1.split())  # str.split() returns LIST
database.append(s2.split())

print(database)

# WHAT IS IN THE LIVINGROOM?
for record in database:  # for index in range(len(database)) ; record = database[index]
    print(record)
    if record[0] == 'BATHROOM':
        print(record[2])
        break
else:      
    print('UNKNOWN')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14967132984999157, 'fraction_numerical': 0.028484746334063712, 'mean_word_length': 4.26063829787234, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, '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': '308272', 'license': 'MIT', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2198, 'n_words': 738, 'path': 'python-3(List and Tuple).ipynb', 'repo_name': 'ShinjiKatoA16/UCSY-sw-eng', 'size': '5959'}