Lists [ ]
append(x): Adds an item x to the end of the list.
extend(iterable): Extends the list by appending all the items from the iterable.
insert(i, x): Inserts an item x at a given position i.
remove(x): Removes the first item from the list whose value is x. Throws a ValueError if x is not found.
pop([i]): Removes the item at the given position i in the list and returns it. If no index is specified, pop() removes and returns the last item in the list.
clear(): Removes all items from the list.
index(x[, start[, end]]): Returns the index of the first item whose value is x. Optional arguments start and end are used to limit the search to a particular subsequence of the list.
count(x): Returns the number of times x appears in the list.
sort(key=None, reverse=False): Sorts the items of the list in place (the arguments can be used for sort customization).
reverse(): Reverses the elements of the list in place.
copy(): Returns a shallow copy of the list.
# Initialize a list
my_list = [1, 2, 3, 2]
# append(x)
my_list.append(4)
print("append:", my_list) # [1, 2, 3, 2, 4]
# extend(iterable)
my_list.extend([5, 6])
print("extend:", my_list) # [1, 2, 3, 2, 4, 5, 6]
# insert(i, x)
my_list.insert(1, 'a')
print("insert:", my_list) # [1, 'a', 2, 3, 2, 4, 5, 6]
# remove(x)
my_list.remove(2)
print("remove:", my_list) # [1, 'a', 3, 2, 4, 5, 6] - removes the first occurrence of 2
# pop([i])
print("pop:", my_list.pop()) # 6
print(my_list) # [1, 'a', 3, 2, 4, 5]
# clear()
my_list.clear()
print("clear:", my_list) # []
# Reinitialize for further examples
my_list = [1, 2, 3, 2, 4]
# index(x[, start[, end]])
print("index:", my_list.index(2)) # 1
# count(x)
print("count:", my_list.count(2)) # 2
# sort(key=None, reverse=False)
my_list.sort(reverse=True)
print("sort:", my_list) # [4, 3, 2, 2, 1]
# reverse()
my_list.reverse()
print("reverse:", my_list) # [1, 2, 2, 3, 4]
# copy()
my_list_copy = my_list.copy()
print("copy:", my_list_copy) # [1, 2, 2, 3, 4]
Tuple ( )
count(x): Returns the number of times x appears in the tuple.
index(x): Returns the index of the first item whose value is x.
# Initialize a tuple
my_tuple = (1, 2, 3, 2)
# count(x)
print("count:", my_tuple.count(2)) # 2
# index(x)
print("index:", my_tuple.index(2)) # 1
Dictionary {key: value}
keys(): Returns a view object displaying a list of all the keys in the dictionary.
values(): Returns a view object displaying a list of all the values in the dictionary.
items(): Returns a view object containing a tuple for each key-value pair in the dictionary.
get(key[, default]): Returns the value for key if key is in the dictionary, else default.
pop(key[, default]): Removes the specified key and returns the corresponding value. If key is not found, default is returned if given, otherwise a KeyError is raised.
popitem(): Removes and returns a (key, value) pair as a tuple from the dictionary. Pairs are returned in LIFO order.
clear(): Removes all items from the dictionary.
update([other]): Updates the dictionary with the key/value pairs from other, overwriting existing keys.
setdefault(key[, default]): Returns the value of the key if it is in the dictionary. If not, it inserts key with a value of default and returns default.
# Initialize a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# keys()
print("keys:", list(my_dict.keys())) # ['name', 'age', 'city']
# values()
print("values:", list(my_dict.values())) # ['John', 30, 'New York']
# items()
print("items:", list(my_dict.items())) # [('name', 'John'), ('age', 30), ('city', 'New York')]
# get(key[, default])
print("get:", my_dict.get('name')) # John
# pop(key[, default])
print("pop:", my_dict.pop('age')) # 30
print(my_dict) # {'name': 'John', 'city': 'New York'}
# popitem()
key, value = my_dict.popitem()
print("popitem:", key, value) # ('city', 'New York')
print(my_dict) # {'name': 'John'}
# clear()
my_dict.clear()
print("clear:", my_dict) # {}
# setdefault(key[, default])
my_dict.setdefault('name', 'Default Name')
print("setdefault:", my_dict) # {'name': 'Default Name'}
# update([other])
my_dict.update({'age': 30})
print("update:", my_dict) # {'name': 'Default Name', 'age': 30}
Set { }
add(elem): Adds an element to the set.
remove(elem): Removes an element from the set. If the element is not a member, raises a KeyError.
discard(elem): Removes an element from the set if it is a member. If the element is not a member, does nothing.
pop(): Removes and returns an arbitrary set element. Raises KeyError if the set is empty.
clear(): Removes all elements from the set.
union(*others) or |: Returns a new set with elements from the set and all others.
intersection(*others) or &: Returns a new set with elements common to the set and all others.
difference(*others) or -: Returns a new set with elements in the set that are not in the others.
symmetric_difference(other) or ^: Returns a new set with elements in either the set or other but not both.
update(*others): Updates the set, adding elements from all others.
intersection_update(*others): Updates the set, keeping only elements found in it and all others.
difference_update(*others): Updates the set, removing elements found in others.
symmetric_difference_update(other): Updates the set, keeping only elements found in either the set or other but not in both.
# Initialize a set
my_set = {1, 2, 3}
# add(elem)
my_set.add(4)
print("add:", my_set) # {1, 2, 3, 4}
# remove(elem)
my_set.remove(2)
print("remove:", my_set) # {1, 3, 4}
# discard(elem)
my_set.discard(3)
print("discard:", my_set) # {1, 4}
# pop()
print("pop:", my_set.pop()) # 1 (or 4, since sets are unordered, any element could be removed)
print(my_set) # Remaining elements
# clear()
my_set.clear()
print("clear:", my_set) # set()
# Reinitialize for operation examples
my_set = {1, 2, 3}
another_set = {3, 4, 5}
# union(*others) or |
print("union:", my_set | another_set) # {1, 2, 3, 4, 5}
# intersection(*others) or &
print("intersection:", my_set & another_set) # {3}
# difference(*others) or -
print("difference:", my_set - another_set) # {1, 2}
# symmetric_difference(other) or ^
print("symmetric_difference:", my_set ^ another_set) # {1, 2, 4, 5}
code example:
shop_menus = ["만두", "떡볶이", "오뎅", "사이다", "콜라"]
shop_orders = ["오뎅", "콜라", "만두"]
def is_available_to_order(menus, orders):
menus_set = set(menus)
for order in orders:
if order not in menus_set:
return False
return True
result = is_available_to_order(shop_menus, shop_orders)
print(result)