List Operations


In [140]:
# Adding together
fruit = ['apple', 'pear', 'orange']
vegetable = ['carrot', 'broccoli', 'celery']

food_fruit_first = fruit + vegetable

print(food_fruit_first)

food_vegetable_first = vegetable + fruit

print(food_vegetable_first)


['apple', 'pear', 'orange', 'carrot', 'broccoli', 'celery']
['carrot', 'broccoli', 'celery', 'apple', 'pear', 'orange']

In [141]:
# Repeating List as characters
na = 'na'
batman = 'batman'
meow = na * 2
nana = meow

print(meow)

print((nana + ' ') * 8 + batman)

nana_with_space = nana + ' '
print('{0}{0}{0}{0} {1}'.format(nana_with_space, batman))


nana
nana nana nana nana nana nana nana nana batman
nana nana nana nana  batman

In [142]:
# Repeating lists
values = [1,2,3,4,5]

def multiply_by_2(value):
    return value * 2

print(values * 2)
print(list(map(multiply_by_2, values)))


[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]

In [143]:
# Removing elements from lists

bag = ['knife', 'chapstick', 'cards', 'plum', 'jacket']

print(bag)

del bag[0]

print(bag)


['knife', 'chapstick', 'cards', 'plum', 'jacket']
['chapstick', 'cards', 'plum', 'jacket']

In [144]:
# Removing elements from lists -- this has an error, be ready
fruit = ['apple', 'pear', 'orange']
vegetable = ['carrot', 'broccoli', 'celery']

food = fruit + vegetable

def is_fruit(item):
    return item in fruit


print('Food starts wtih', food)
# Remove all the fruit we've added -- or so we thought
for index in range(len(food)):
    print('I\'m at list location', index)
    print('Length of list', len(food))
    if is_fruit(food[index]):
        print('Deleted', food[index], 'index', index)
        del food[index]
        

print('Food ends with', food)


Food starts wtih ['apple', 'pear', 'orange', 'carrot', 'broccoli', 'celery']
I'm at list location 0
Length of list 6
Deleted apple index 0
I'm at list location 1
Length of list 5
Deleted orange index 1
I'm at list location 2
Length of list 4
I'm at list location 3
Length of list 4
I'm at list location 4
Length of list 4
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-144-7b5052d9c021> in <module>()
     14     print('I\'m at list location', index)
     15     print('Length of list', len(food))
---> 16     if is_fruit(food[index]):
     17         print('Deleted', food[index], 'index', index)
     18         del food[index]

IndexError: list index out of range

In [145]:
# Better way of removing from the list

# Removing elements from lists
fruit = ['apple', 'pear', 'orange']
vegetable = ['carrot', 'broccoli', 'celery']

food = fruit + vegetable

print('Food starts wtih', food)
# Remove all the fruit we've added
for item in fruit:
    if item in food:
        food.remove(item)        

print('Food ends with', food)

numbers = [1,2,3,4,1,2,3,4]
print(numbers)
numbers.remove(1)
print(numbers)
numbers.remove(1)
print(numbers)
numbers.remove(1)


Food starts wtih ['apple', 'pear', 'orange', 'carrot', 'broccoli', 'celery']
Food ends with ['carrot', 'broccoli', 'celery']
[1, 2, 3, 4, 1, 2, 3, 4]
[2, 3, 4, 1, 2, 3, 4]
[2, 3, 4, 2, 3, 4]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-145-8f16409a7e80> in <module>()
     21 numbers.remove(1)
     22 print(numbers)
---> 23 numbers.remove(1)

ValueError: list.remove(x): x not in list

Objects and memory

Think about the is operator as well as the == operator. Also look at how memory is allocated for a computer.


In [146]:
# Looking at the is operator
def default_return():
    pass


print(default_return() is None)


True

In [ ]:


In [147]:
first_list = []
second_list = []

print(first_list)
print(second_list)
print('Equal', first_list == second_list)
print('The same', first_list is second_list)
print(id(first_list), id(second_list))


[]
[]
Equal True
The same False
4500354312 4500354824

In [148]:
first_a  = 'a'
second_a = 'a'

print('Equal', first_a == second_a)
print('The same', first_a is second_a)
print(id(first_a), id(second_a))


Equal True
The same True
4472724032 4472724032

In [149]:
first_int = 1
second_int = 1

print('Equal', first_int == second_int)
print('The same', first_int is second_int)
print(id(first_int), id(second_int))


Equal True
The same True
4468480560 4468480560

Adding to lists


In [150]:
def starts_with_a(word):
    if type(word) is str:
        return word.startswith('a')

    
foods_bar = ['apple', 'anise', 'blueberry', 'pizza', 'chips']
foods_foo = ['chicken', 'apricote', 'charmander']
foods_starting_with_a = []
    
for food in foods_bar:
    if starts_with_a(food):
        foods_starting_with_a.append(food)

for food in foods_foo:
    if starts_with_a(food):
        foods_starting_with_a.append(food)

print(foods_starting_with_a)


['apple', 'anise', 'apricote']

In [151]:
numbers = [1,2,3,4]
numbers_two = [5,6,7,8]

print(numbers)
print(numbers_two)

starting_numbers = [list(numbers), numbers_two]
print(starting_numbers)


print(id(starting_numbers))
print(id(numbers))
print(id(starting_numbers[0]))

numbers.append(200)

print(numbers)
print(starting_numbers)


[1, 2, 3, 4]
[5, 6, 7, 8]
[[1, 2, 3, 4], [5, 6, 7, 8]]
4500724296
4500725256
4500743944
[1, 2, 3, 4, 200]
[[1, 2, 3, 4], [5, 6, 7, 8]]

Assignment and how it works

Using adding to lists to show you can modify one another.


In [152]:
from pprint import pprint

fruit = ['apple', 'pear', 'grape']
non_perishable = ['chips', 'soup', 'cans']
meat = ['bologna', 'beef', 'turkey']

warehouse_foods = [
    fruit,
    non_perishable
]


material = ['cotton', 'hide', 'denim']
made_goods = ['gloves', 'boots', 'jeans']

warehouse_goods = [
    material,
    made_goods
]

warehouse_foods_and_goods = warehouse_foods + warehouse_goods

# Print out the lists of all foods and goods
print('Warehouse of foods and goods')
pprint(warehouse_foods_and_goods)
print()

# Change the food wharehouse to have another set of food
warehouse_foods.append(meat)
print('Warehouse of foods')
pprint(warehouse_foods)
print()

# See what's in the food and goods wharehouse again
print('Warehouse of foods and goods')
pprint(warehouse_foods_and_goods)
print()

# Add more fruit
fruit.append('peach')
print('Fruits')
print(fruit)
print()

# See what's in the food warehouse
print('Food warehouse')
pprint(warehouse_foods)
print()

# See what's in the food and goods ware house
print('Food and goods')
pprint(warehouse_foods_and_goods)
print()

# Remove the materials from the warehouse_goods
del warehouse_goods[1]
print('Warehouse of goods')
pprint(warehouse_goods)
print()

# See what's left in the food and goods warehouse
print('Warehouse of Foods and Goods')
pprint(warehouse_foods_and_goods)
print()


Warehouse of foods and goods
[['apple', 'pear', 'grape'],
 ['chips', 'soup', 'cans'],
 ['cotton', 'hide', 'denim'],
 ['gloves', 'boots', 'jeans']]

Warehouse of foods
[['apple', 'pear', 'grape'],
 ['chips', 'soup', 'cans'],
 ['bologna', 'beef', 'turkey']]

Warehouse of foods and goods
[['apple', 'pear', 'grape'],
 ['chips', 'soup', 'cans'],
 ['cotton', 'hide', 'denim'],
 ['gloves', 'boots', 'jeans']]

Fruits
['apple', 'pear', 'grape', 'peach']

Food warehouse
[['apple', 'pear', 'grape', 'peach'],
 ['chips', 'soup', 'cans'],
 ['bologna', 'beef', 'turkey']]

Food and goods
[['apple', 'pear', 'grape', 'peach'],
 ['chips', 'soup', 'cans'],
 ['cotton', 'hide', 'denim'],
 ['gloves', 'boots', 'jeans']]

Warehouse of goods
[['cotton', 'hide', 'denim']]

Warehouse of Foods and Goods
[['apple', 'pear', 'grape', 'peach'],
 ['chips', 'soup', 'cans'],
 ['cotton', 'hide', 'denim'],
 ['gloves', 'boots', 'jeans']]


In [153]:
my_list = [2, 5, 3, 1]
# run first iteration bubble
my_list = [2, 3, 5, 1]
# run second iteraiton bubble
my_list = [2, 3, 1, 5]
# run third iteration bubble
my_list = [2, 1, 3, 5]
# run fourth iteration bubble
my_list = [1, 2, 3, 5]
# run fifth iteration bubble
# DONE my_list is sorted

In [154]:
# Bubble Sort implementation:

def bubble_sort (items):
    is_sorted = False
    while is_sorted is False:
        nswaps = 0
        for index in range(1, len(items)):
            previous_index = index - 1
            
            print(items)
            if items[previous_index] > items[index]:
                current_item = items[index]
                previous_item = items[previous_index]
                
                items[previous_index] = current_item
                items[index] = previous_item
                
                nswaps =+ 1
            print(items)
        if nswaps is 0:
            is_sorted = True
    return items

list_1 = [1, 2, 3, 4, 5]
list_2 = [5, 4, 3, 2, 1]
list_3 = [4, 5, 3, 1, 2]
print(bubble_sort(list(list_1)))
print(bubble_sort(list(list_2)))
print(bubble_sort(list(list_3)))


assert bubble_sort(list(list_1)) == sorted(list(list_1))
assert bubble_sort(list(list_2)) == sorted(list(list_2))
assert bubble_sort(list(list_3)) == sorted(list(list_3))


[1, 2, 3, 4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