Python_Chap_4 Cheat Sheet

A Programming › Python cheat sheet in English by Theo666

lists : a collection of items that are ordered and mutable

3,258 views 794 downloads 17 Sep 2023 History

Lists

 
A list is a data structure that contains a series of values. Python allows the constr­uction of a list containing values of different types. It's iterable, indexable, ordered, mutable and not hashable.

combin­ation of lists and dictio­naries

[[...], [...], [...], ...]
list of list
list_o­f_l­ist­[...][...]
index elements in a list of list
dict{key : [..., ...]}
values of a dictionary can be a list
[dict1, dict2]
list of dictionary
But the keys of dictionary are hasables, not ordered and not duplic­ated.
list[:] is a new list duplic­ating the original, so list[:][0] will return the first element of the list

Operation in the lists

list + list
add lists
list * int
multiply the list
- We cannont substract directly a list from another by using ' - ' . [ i for i in list1 if i not in list2]; set(list1) - set(list2)
- To repeat each element of lists, [i for i in list for _ in range(­int)]
- list_1 += list_2 equal to list_1 = list_1 + list_2

Examples

[i for i in range(10)]
[i for i in range(31) if i % 2 == 0]
[[m.upper(), len(m)] for m in msg_lst]
[seq[i:i+width] for i in range(0, len(seq), width)]

Common Functions

list[s­tar­t:s­top­:step]
list sciling (tranche)
enumer­ate­(list)
return positions and items
list.i­nde­x(item)
return the position of the item
list.c­oun­t(item)
returns the number of times that element appears in the list
list(s­tring)
convert a string to a list one by one charcter
string.sp­lit­(sep)
convert a string to a list with a separator
'­sep­'.jo­in(­list)
convert a list to a string
len(list)
length of lists
max(list)
find the maximun
min(list)
find the minimun
sum(list)
calculate the sum
list.s­ort­(re­verse=)
sort the elements of a list in-place. reverse False from smallest to largest values; alphabetic order possible
sorted­(list, reverse=)
create a new sorted list without modifying the original list; alphabetic order possible
list.r­eve­rse()
reverse the elements of a list in-place
revers­ed(­list)
create a new reversed list without modifying the original list
list.a­ppe­nd(­item)
add an element to the end of lists
list.i­nse­rt(­item, pos)
insert an element at a position of lists
list.r­emo­ve(­item)
remove an item from lists; remove only one first element.
list.pop()
remove and return the last element
del list[pos]
remove the item by its position idenx
range(­sta­rt,­sto­p,step)
similar to lists, but immutable. stop at n-1
set(list)
remove the duplicated elements
- list[s­tar­t:s­top­:step] step 1 by default; stop at n-1 even if negative index
- list[:] create a new list. lst2 = lst1 creates a reference to the original list with the same ID
- '­sep­'.jo­in(­list) cannot combine a list containing only number (int & float). [str(i) for i in list]
- list.r­emo­ve(­item) If there're duplicated elements, it remove only the first element
- range(­sta­rt,­sto­p,step) stop could be higher than start with a negative step

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.