Data Structures Flashcards
“Type” command
Returns the class of the data, or the exact type if a Python object
Ex
Type(5)
Class ‘int’
Int, str, float
Sequences
An ordered collection of values.
Sequence types:
Strings
Tuples
Lists
Tuple
A tuple is an immutable sequence of 0 or more values.
Enclosed in round brackets and separated by commas.
Tuple functions
X in tup. - True if x is an element in tup
Len(tup) - # of elements in tup
tup.count(x) - # of times element x occurs in tup
Tup.Index(x) - index location of first occ of x, returns ValueError exception if not in tup
Lists
Lists are the same as tuples, but they are mutable- ie they can be changed.
Separated by commas, enclosed in square brackets.
Can use len, concatenate, indexing, slicing.
List functions
Most are mutating functions, ie they modify the list.
S.append (x)
Appends x to the end of s
What does “pop” refer to?
The act of removing the last element of a list.
What does “push “ refer to?
Adding an element to the same end of a list (like append)
What does “stack “ refer to?
When push and pop are used on the same list.
Items are pushed onto the top of the stack, and then popped from the top of the stack.
S.count(x)
Returns the number of times x appears in s
S.extend(lst)
Appends each item of “lst” to s
S.pop(i)
Removes and returns the item at index i in s
S.index(x)
Returns the index value of the leftmost occurrence of x
S.insert(i, x)
Inserts x before index location i (so that s[i] == x)
S.remove(x)
Removes the leftmost occurrence of x in s
S.reverse()
Reverses the order of the elements of s
S.sort()
Sorts the elements of s into increasing order
What is lexicographical ordering?
The order in which Python sorts a list of sequences. This is a general term meaning “alphabetical order “. It applies to any sequence of orderable values.
List comprehensions
A special notation for creating lists
> > > [n * n for n in range(1, 11):
Creates list of squares from 1-10
Filtered comprehensions
List comprehensions can filter out elements you don’t want.
> > > result = [n for n in nums if n > 0]
Returns only positive numbers
What is a Python dictionary?
This is an extremely efficient data structure for storing pairs of values in the form key:value
Also referred to as associative arrays, maps, or hash tables.
> > > color = {‘red’ : 1, ‘blue’ : 2, ‘green’ : 3}
‘color’ is the dictionary name, has three members, one is ‘blue : 2’, where ‘blue’ is the key and ‘2’ is the associated value.
Key restrictions
Keys have to be unique. You can’t have more than one key:value pair in the same dictionary.
Keys must be immutable, values can be mutable.
Dictionary function
d.items()
Returns a view of the (key, value) pairs in d
Dictionary function
d.keys()
Returns a view of the keys of d
Dictionary function
d.values()
Returns a view of the values in d
Dictionary function
d,get(key)
Returns the value associated with key
Dictionary function
d.pop(key)
Removes key and returns its corresponding value
Dictionary function
d.popitem()
Returns some (key, value) pair from d
Dictionary function
d.clear()
Removes all items from d
Dictionary function
d.copy()
Creates a copy of d
Dictionary function
d.fromkeys(s, t)
Creates a new dictionary with keys taken from s and values taken from t
Dictionary function
d.setdefault(key, v)
If key is in d, returns its value; if key is not in d, returns v and adds (key, v) to d
Dictionary function
d.update(e)
Adds the (key, value) pairs in e to d