You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can use the tuple() function to return a tuple version of the value passed to it
and similarly the list() function to convert to a list:
a_tuple = (1,2,3,4,5,6,7,8,9,10)
b_list = [1,2,3,4,5]
print(tuple(b_list))
print(list(a_tuple))
bin(n) # 2
oct(n) # 8
hex(n) # 16
Dictionaries
dictionary as a set of key: value pairs,
with the requirement that the keys are unique (within one dictionary).
A pair of braces creates an empty dictionary: {}.
dictionaries are indexed by keys
keys can be any immutable type; strings and numbers can always be keys
Tuples can be used as keys if they contain only strings, numbers, or tuples;
A set is an unordered collection with no duplicate elements
*** Note: to create an empty set you have to use set(), not {}; ***
Curly braces or the set() function can be used to create sets.
- a = set('abracadabra') # {'a', 'r', 'b', 'c', 'd'}
- basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> a = set('abracadabra') # unique letters in a {'a', 'r', 'b', 'c', 'd'}
>>> b = set('alacazam')
>>> a - b # letters in a but not in b {'r', 'd', 'b'}
>>> a | b # letters in a or b or both {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b {'a', 'c'}
>>> a ^ b # letters in a or b but not both {'r', 'd', 'b', 'm', 'z', 'l'}
>>> a = {x for x in 'abracadabra' if x not in 'abc'} # {'r', 'd'}
Lists
- slices are quite useful when making copies of lists
- list comprehension is used everywhere
when iterating over lists, strings, ranges and even more data types!
- nested lists are essential for building more complex data structures
like matrices, game boards and mazes
- swapping is quite useful when shuffling or sorting
[bool(val) for val in [0, [], '']] # [False, False, False]
numbers = [1, 2, 3, 4, 5, 6]
evens = [num for num in numbers if num % 2 == 0]
odds = [num for num in numbers if num % 2 != 0]
[num*2 if num % 2 == 0 else num/2 for num in numbers] # [0.5, 4, 1.5, 8, 2.5, 12]
nested list
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[print(val) for val in l] for l in nested_list]
how to write a nest list
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[] for list in nested_list] # step 1
[[for x in list] for list in nested_list] # step 2
print(string[::-1]) # reverse a string
a[len(a):] = [x]
list.append(x) # stack
pop([i]) # stack
string.strip() # remove white space
insert, remove or sort that only modify the list have no return value printed
[1] * num_one + [2] * num_two + [3] * num_three
they return the default None.
This is a design principle for all mutable data structures in Python.
Using Lists as Stacks
- (“last-in, first-out”)
To add an item to the top of the stack, use append().
To retrieve an item from the top of the stack, use pop() without an explicit index.
- (“first-in, first-out”)
however, lists are not efficient for this purpose.
While appends and pops from the end of list are fast,
doing inserts or pops from the beginning of a list is slow
(because all of the other elements have to be shifted by one).
*** To implement a queue, use collections.deque
- collections.deque # use collections.deque
>>> from collections import deque
>>> queue = deque(["Xing", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Xing'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
A tuple consists of a number of values separated by commas, for instance:
Though tuples may seem similar to lists,
they are often used in different situations and for different purposes.
Tuples are immutable,
and usually contain a heterogeneous sequence of elements that are accessed via unpacking
(see later in this section) or indexing (or even by attribute in the case of namedtuples).
Lists are mutable,
and their elements are usually homogeneous and are accessed by iterating over the list.