any_list = [
[1,2,3],
[4,5,6],
[7,8,9],
[[10,11,12], 13, 14]
];
any_list[3]; # equals [[10,11,12], 13, 14]
any_list[3][0]; # equals [10,11,12]
any_list[3][0][1]; # equals 11
.append() is a method that adds an item to the end of a list
arr = [1,2,3];
arr.append(4); # arr is now [1,2,3,4]
.pop() is a method that "pop"s the last item of a list. You can store this popped item in variable
three = [1, 4, 6];
remove_one = three.pop();
print(remove_one); # Returns 6
print(three); # Returns [1, 4]
You can use "+" to add lists together. Just make sure to reassign the new list to a var
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7]
list1 + list2
print(list1)
> [1, 2, 3, 4]
list1 = list1 + list2
print(list1)
> [1, 2, 3, 4, 5, 6, 7]
.insert() is a method that inserts an item into a list according to the desired list index
goats = ["kobe", "mj", "magic"]
goats.insert(2, "larry")
print(goats)
["kobe", "mj", "larry", "magic"]
Use the index number of list items to amend specific items in a list
def merge_and_swap(list1, list2):
list1 = list1 + list2
list1[0], list1[-1] = list1[-1], list1[0]
return list1
print(merge_and_swap([1, 2, 3, 4, 5], [6, 7, 8, 9]))
[9, 2, 3, 4, 5, 6, 7, 8, 1]
list_name[start: stop: step]
The third value (step) has a default value of 1. You can also work with a copy of a list using [:]. A key takeaway here is that when you slice a list, you don't actually edit the original list. Instead, you create a new one.
bts = ["RM", "제이홉", "진", "정국", "지민", "뷔", "슈가"]
sub_bts = bts[1:4]
print(f"bts = {bts}")
> bts = ['RM', '제이홉', '진', '정국', '지민', '뷔', '슈가']
print(f"sub_bts = {sub_bts}")
> sub_bts = ['제이홉', '진', '정국']
An important difference between lists and tuples is that tuples are immutable (items cannot be changed). You can still add tuples together and retrieve stored items in a tuple using the same methods used for lists
my_tuple = (1, 2, 3, 4, 5, 6, 7, 8)
new_tuple = my_tuple[:] + (5, 5, 5)
print(new_list)
(1, 2, 3, 4, 5, 6, 7, 8, 5, 5, 5)
Key takeaway: lists use more memory than tuples, so when dealing with items that do not need to be changed, use tuples instead of lists
Key takeaways:
Use .add() to add an item to a set
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
> {1, 2, 3, 4}
Use .remove() to remove an item from a setmy_set = {1, 2, 3}
my_set.remove(3)
print(my_set)
> {1, 2}
"&" and intersection() show intersecting items between two sets
set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
print(set1 & set2)
> {4, 5, 6}
print(set1.intersection(set2))
> {4, 5, 6}
"|" and .union() unites two sets together
print(set1 | set2)
> {1, 2, 3, 4, 5, 6, 7, 8, 9}
print(set1.union(set2))
> {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dictionaries consist of key: value pairs
dict1 = { 1 : "one", 1 : "two" }
print(dict1)
> { 1: "two" }
dictionary_name[new_key] = new_value
my_dict = { "one": 1, 2: "two", 3 : "three" }
my_dict["four"] = 4
print(my_dict)
> {'one': 1, 2: 'two', 3: 'three', 'four': 4}
Use del
my_dict = { "one": 1, 2: "two", 3 : "three" }
del my_dict["one"]
print(my_dict)
> {2: 'two', 3: 'three'}
Loop through a dictionary using .keys(), .values(), or .items()
.keys() - returns dict keys in a list
.values() - returns dict values in a list
.items() - returns key:value pairs in tuples (within a surrounding list)
my_dict = {"name": "mark", "age": 24, "location": "seoul"}
print(my_dict.keys())
print(my_dict.values())
print(my_dict.items())
dict_keys(['name', 'age', 'location'])
dict_values(['mark', 24, 'seoul'])
dict_items([('name', 'mark'), ('age', 24), ('location', 'seoul')])