Lekcja – Listy, słowniki, zbiory, tuple, kiedy z czego korzystać?

 
print(range(10))

print(list(range(10, 0, -1)))

t1 = (1,2)
t1

t1[1]

t2 = (1, 2, 'green', ['Berlin','Warsaw','Paris'])
t2

t2[3][2]

(*t2, 99)

(t2 + (99,))

healthy = {'vegetables', 'bread', 'milk', 'eggs'}

drinks = {'beer', 'caffe', 'tea', 'milk'}

'milk' in drinks

'chips' in healthy

healthy | drinks

healthy & drinks

pricelist = {
'milk' : 2,
'bread': 4,
'butter': 3
}

pricelist

for k,v in pricelist.items():
   print(k, v)

for k in pricelist.keys():
   print(k, pricelist[k])

pricelist['bread']

Rozwiązanie:

 
import time

max_limit = 50000

my_list = list(range(max_limit))
my_set = set(range(max_limit))

# checking time for list search

start = time.time()

for p in range(max_limit):
   is_present = p in my_list

stop = time.time()

print(f'List search operation duration was {stop - start}')

# checking time for set search

start = time.time()

for p in range(max_limit):
   is_present = p in my_set

stop = time.time()

print(f'Set search operation duration was {stop - start}')