Data Structures

Run Settings
LanguagePython
Language Version
Run Command
## Lists amazon_cart = [ 'notebooks', 'sunglasses', 'toys', 'grapes', 'bikes' ] print(amazon_cart[1]) # sunglasses # List slicing - note that list slicing produces a new list and does not change the original one print(amazon_cart[1:4:2]) # sunglasses, grapes - The stop parameter is not included so 1:4 means indexes 1, 2, 3 print(amazon_cart[0::2]) # Prints all items with even indexes # Lists are muttable unlike strings: amazon_cart[0] = 'laptops' print(amazon_cart) new_cart = amazon_cart[:] # This notation creates a copy of the amazon list and assigns it to the new_cart. # Writing new_cart = amazon_cart will make both point to the samelist meaning that changes in new_cart will cause changes in the # amazon_cart since they both actually the same object # Matrix matrix = [ [1, 2, 3], [2, 4, 6], [7, 8, 9] ] print(matrix[1][1]) # second row, second column basket = [1, 2, 3, 4, 5] # list's length: print(len(basket)) # adding items - append - changes the original list. Not creating a new one basket.append(6) print(basket) # adding - insert - changes the original list. Not creating a new one # The insert method adds the given object in the given index basket.insert(1, 7) # insert the value 7 in the second place print(basket) # Extend - Adding a sequence of itmes (iterable) to an existing list: basket.extend([8, 9]) print(basket) # Remove item. The pop function returns the removed object basket.pop() # Pops the value in the end of the list. To remove a value in a specific index we specify a value in the parenthesis: basket.pop(0) # removes the first item in the list print(basket) # To remove an item according to its value and not its index we use the remove method: basket.remove(7) # The remove method does not return anything. Just modifies the list print(basket) basket.clear() # removes all the items in the list creating an empty list (does not return any value) print(basket) basket = ['a', 'b', 'c', 'd', 'e', 'd'] # Getting the index of the first object with the specified value: # print(basket.index('d', 1, 3)) # get the index of the character 'd'. Start looking from index 1 to index 3 # (3 is not included so we will get a message that the item is not in the list) # Another way of checking existance: print('x' in basket) # Returns False print(basket.count('d')) # How many times the letter 'd' appears in the list (1) basket.sort() # Modifies the list print(basket) # In order to sort without modifying the list we use the sorted built=in function: basket = ['a', 'x', 'f', 'd', 'b'] print(sorted(basket)) print(basket) # reversing the items in the list (not sorting it in the opposite direction): basket.reverse() # Modifies the list print(basket) print(basket[::-1]) # returns a reversed list without modifying the original list print(basket) # Creating a list with values from 1 to 99: print(list(range(1, 100))) # start included, stop is not included # Combining a list into a string: print(' '.join(['Hi,', 'my', 'name', 'is', 'Eran'])) # This adds the space between all items in the list # List unpacking a, b, c, *other, d = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(a) # 1 print(b) # 2 print(c) # 3 print(other) # [4, 5, 6, 7, 8] print(d) # 9 # Dictionary dictionary = { 'a': 1, 'b': 2, 'x': 3 } print(dictionary['b']) # print the value of 'b' (2) dictionary = { 123: [1, 2, 3], True: 'hello' } print(dictionary[123]) print(dictionary[True]) # Tuple - like immutable list. Since their immutable they have better performance my_tuple = (1, 2, 3, 4, 5, 5) print(my_tuple[4]) # Ok. Prints 5 print(5 in my_tuple) # Ok. Prints True # my_tuple[4] = 7 # Not Ok. Tuples are immutable # It is Ok to assign a tuple (or part of it) to a new tuple: new_tuple = my_tuple[1:3] print(new_tuple) # Prints (2, 3) x, y, z, *other = (1, 2, 3 ,4, 5, 5) print(x) print(y) print(z) print(other) print(my_tuple.count(5)) # The count function returns the number of appearances of the given value print(my_tuple.index(5)) # The index function returns the index of the given value ### Sets my_first_set = {1, 2, 3, 4, 5, 5} print(my_first_set) # Prints {1, 2, 3, 4, 5} since all items in the set must be unique new_set = my_first_set.copy() print(len(my_first_set)) print(list(my_first_set)) # my_first_set.clear() - Clears the set ## Important set methods: # a.difference(b) - values that exists in a and not in b # a.discard(value) - removes value from the set (if exists). It does not return anything (None) but modifies the set # a.difference_update(b) - removes all values from b set that exists in a set # a.intersection(b) - returns a set that contains all values from b set that exists in a set. Can also be written like this: a & b # a.isdisjoint(b) - are sets a and b have any value in common. True if they have nothing in common. False otherwise # a.issubset(b) - is set a, a subset of set b # a.issuperset(b) - is set b, a subset of a # a.union(b) - returns all unique values from both sets. Can also be written like this: a | b my_set = {1, 2, 3, 4, 5} my_set2 = {1, 2, 3, 4, 5} my_set3 = {4, 5} your_set = {4, 5, 6, 7, 8, 9, 10} print(my_set.difference(your_set)) print(my_set.discard(5)) print(my_set) print(my_set.difference_update(your_set)) print(my_set) print(my_set2.intersection(your_set)) # can also be written like this: print(my_set2 & your_set) print(my_set2 & your_set) # The same as intersection print(my_set.isdisjoint(your_set)) print(my_set.issubset(your_set)) print(my_set3.issuperset(your_set)) print(your_set.issuperset(my_set3)) print(my_set.union(your_set)) # can also be written like this: print(my_set2 | your_set) print(my_set2 | your_set) # The same as union
Editor Settings
Theme
Key bindings
Full width
Lines