COMMANDS
pip freeze >
requirments .txt
|
|
|
STRING METHODS
|
String to uppercase |
|
String to lowercase |
|
Occurrences of element |
|
Strip whitespaces |
|
Returns True if |
|
Position of first occurrence |
|
Returns True if all are digits |
SLICING
list
[
Initial : End : IndexJump
]
|
my_list
[1:3] Select items at index 1 and 2 |
my_list
[1:] Select items after index 0 |
my_list
[:3] Select items before index 3 |
|
|
|
LISTS
|
|
Adds 6 to the end |
|
Adds 6 at index position of 0 |
|
Removes 6 |
|
Removes the last item |
|
Removes all the items |
|
Returns the index of first 8 |
|
Sorts the list |
|
Reverses the list |
|
Returns a copy of the list |
|
Deletes elements in range |
|
Extend existing list with list b
|
ADVANCE LISTS
Removing duplicates
Use dict .fromkeys() function to convert list into dictionary with list elements as keys. Then convert the dictionary back to list.
list =[‘a’, ’b’, ’c’, ’b’, ’a’]
list_2 = list (dict .fromkeys (list ))
Filtering a list
list = [item for item in m_list if item > 20]
Combining lists
Each item from list A is combined with corresponding elements from list B in the form of a tuple.
comb_list = list (zip (list_1, list_2 ))
Finding the most common item
list = ['a','a','a','d','d','e']
frequent = max (set
(list),
key=
list .count)
Flatten a list of lists
comprehension-to convert a list of lists into single list.
list=
[item for List in listOflist for item in List ] |
|
|
PYTHON
this is it if than. |
are you serious? |
RANGE
Returns sequence from 0 to 4
|
Returns a sequence form 2000 to 2017
|
Returns seq. from 0 to 10 with increment of 2
|
Returns a sequence from 0 to -9
|
Returns a list from 0 to 4
|
|