0% found this document useful (0 votes)
407 views1 page

Finxter Python Cheat Sheet Complex Data Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
407 views1 page

Finxter Python Cheat Sheet Complex Data Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Cheat Sheet: Complex Data Types

“A puzzle a day to learn, code, and play” → Visit finxter.com


Description Example

List A container data type that stores a l = [1, 2, 2]


sequence of elements. Unlike strings, lists print(len(l)) # 3
are mutable: modification possible.

Adding Add elements to a list with (i) append, (ii) [1, 2, 2].append(4) # [1, 2, 2, 4]
elements insert, or (iii) list concatenation. [1, 2, 4].insert(2,2) # [1, 2, 2, 4]
The append operation is very fast. [1, 2, 2] + [4] # [1, 2, 2, 4]

Removal Removing an element can be slower. [1, 2, 2, 4].remove(1) # [2, 2, 4]

Reversing This reverses the order of list elements. [1, 2, 3].reverse() # [3, 2, 1]

Sorting Sorts a list. The computational complexity [2, 4, 2].sort() # [2, 2, 4]


of sorting is superlinear in the no. list
elements.

Indexing Finds the first occurrence of an element in [2, 2, 4].index(2) # index of element 2 is "0"
the list & returns its index. Can be slow as [2, 2, 4].index(2,1) # index of el. 2 after pos 1 is "1"
the whole list is traversed.

Stack Python lists can be used intuitively as stack = [3]


stacks via the two list operations append() stack.append(42) # [3, 42]
and pop(). stack.pop() # 42 (stack: [3])
stack.pop() # 3 (stack: [])

Set A set is an unordered collection of unique basket = {'apple', 'eggs', 'banana', 'orange'}
elements (“at-most-once”). same = set(['apple', 'eggs', 'banana', 'orange'])

Dictionary The dictionary is a useful data structure for calories = {'apple' : 52, 'banana' : 89, 'choco' : 546}
storing (key, value) pairs.

Reading and Read and write elements by specifying the print(calories['apple'] < calories['choco']) # True
writing key within the brackets. Use the keys() and calories['cappu'] = 74
elements values() functions to access all keys and print(calories['banana'] < calories['cappu']) # False
values of the dictionary. print('apple' in calories.keys()) # True
print(52 in calories.values()) # True

Dictionary You can access the (key, value) pairs of a for k, v in calories.items():
Looping dictionary with the items() method. print(k) if v > 500 else None # 'choco'

Membership Check with the ‘in’ keyword whether the basket = {'apple', 'eggs', 'banana', 'orange'}
operator set, list, or dictionary contains an element. print('eggs' in basket) # True
Set containment is faster than list print('mushroom' in basket) # False
containment.

List and Set List comprehension is the concise Python # List comprehension
Comprehens way to create lists. Use brackets plus an l = [('Hi ' + x) for x in ['Alice', 'Bob', 'Pete']]
ion expression, followed by a for clause. Close print(l) # ['Hi Alice', 'Hi Bob', 'Hi Pete']
with zero or more for or if clauses. l2 = [x * y for x in range(3) for y in range(3) if x>y]
print(l2) # [0, 0, 2]
Set comprehension is similar to list # Set comprehension
comprehension. squares = { x**2 for x in [0,2,4] if x < 4 } # {0, 4}

You might also like