DATA STRUCTURES Flashcards
How do you create an empty list?
empty_list_1 = []
or
empty_list_2 = list()
phrase = [‘Astra’, ‘inclinant’, ‘sed’, ‘non’, ‘obligant’]
print(phrase[1])
inclinant
phrase = [‘Astra’, ‘inclinant’, ‘sed’, ‘non’, ‘obligant’]
print(phrase[-1])
obligant
phrase = [‘Astra’, ‘inclinant’, ‘sed’, ‘non’, ‘obligant’]
print(phrase[1:4])
[‘inclinant’, ‘sed’, ‘non’]
phrase = [‘Astra’, ‘inclinant’, ‘sed’, ‘non’, ‘obligant’]
print(phrase[:3])
print(phrase[3:])
[‘Astra’, ‘inclinant’, ‘sed’]
[‘non’, ‘obligant’]
my_list = [‘Macduff’, ‘Malcolm’, ‘Duncan’, ‘Banquo’]
my_list[2] = ‘Macbeth’
print(my_list)
[‘Macduff’, ‘Malcolm’, ‘Macbeth’, ‘Banquo’]
my_list = [‘Macduff’, ‘Malcolm’, ‘Macbeth’, ‘Banquo’]
my_list[1:3] = [1, 2, 3, 4]
print(my_list)
[‘Macduff’, 1, 2, 3, 4, ‘Banquo’]
num_list = [1, 2, 3]
char_list = [‘a’, ‘b’, ‘c’]
num_list + char_list
[1, 2, 3, ‘a’, ‘b’, ‘c’]
list_a = [‘a’, ‘b’, ‘c’]
list_a * 2
[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]
num_list = [2, 4, 6]
print(5 in num_list)
print(5 not in num_list)
False
True
my_list = [0, 1, 1, 2, 3]
variable = 5
my_list.append(variable)
print(my_list)
[0, 1, 1, 2, 3, 5]
my_list = [‘a’, ‘b’, ‘d’]
my_list.insert(2, ‘c’)
print(my_list)
[‘a’, ‘b’, ‘c’, ‘d’]
my_list = [‘a’, ‘b’, ‘d’, ‘a’]
my_list.remove(‘a’)
print(my_list)
[‘b’, ‘d’, ‘a’]
my_list = [‘a’, ‘b’, ‘c’]
print(my_list.pop())
print(my_list)
c
[‘a’, ‘b’]
pop() removes and returns the last item in the list:
my_list = [‘a’, ‘b’, ‘c’]
my_list.clear()
print(my_list)
[]
my_list = [‘a’, ‘b’, ‘c’, ‘a’]
my_list.index(‘a’)
0
my_list = [‘a’, ‘b’, ‘c’, ‘a’]
my_list.count(‘a’)
2
char_list = [‘b’, ‘c’, ‘a’]
num_list = [2, 3, 1]
char_list.sort()
num_list.sort(reverse=True)
print(char_list)
print(num_list)
[‘a’, ‘b’, ‘c’]
[3, 2, 1]
print(list(‘rocks’))
print(list((‘stones’, ‘water’, ‘underground’)))
[‘r’, ‘o’, ‘c’, ‘k’, ‘s’]
[‘stones’, ‘water’, ‘underground’]
num_list = [1, 2, 3]
num_list[0] = 5446
print(num_list)
[5446, 2, 3]
test1 = (1)
test2 = (2,)
print(type(test1))
print(type(test2))
<class ‘int’>
<class ‘tuple’>
Note: When using parentheses to declare a tuple with just a single element, you must use a trailing comma.
tuple1 = 1,
tuple2 = 2, 3
print(type(tuple1))
print(type(tuple2))
<class ‘tuple’>
<class ‘tuple’>
Common uses of tuples include:
Returning multiple values from a function
Packing and unpacking sequences: You can use tuples to assign multiple values in a single line of code.
Dictionary keys: Because tuples are immutable, they can be used as dictionary keys, whereas lists cannot. (You’ll learn more about dictionaries later.)
Data integrity: Due to their immutable nature, tuples are a more secure way of storing data because they safeguard against accidental changes.
Methods:
Because tuples are built for data security, Python has only two methods that can be used on them:
count() returns the number of times a specified value occurs in the tuple.
index() searches the tuple for a specified value and returns the index of the first occurrence of the value.
cities = [‘Paris’, ‘Lagos’, ‘Mumbai’]
countries = [‘France’, ‘Nigeria’, ‘India’]
places = zip(cities, countries)
print(places)
print(list(places))
<zip object at 0x7f6a4f94b8c8>
[(‘Paris’, ‘France’), (‘Lagos’, ‘Nigeria’), (‘Mumbai’, ‘India’)]
____________________________________
Notice that, in this case, the list() function is used to generate a list of tuples from the iterator object. Here are a few things to keep in mind when using the zip() function.
It works with two or more iterable objects. The given example zips two sequences, but the zip() function will accept more sequences and apply the same logic.
If the input objects are of unequal length, the resulting iterator will be the same length as the shortest input.
If you give it only one iterable object as an argument, the function will return an iterator that produces tuples containing only one element from that iterable at a time.