Show Menu

Collection Data Types Cheat Sheet by

python     beginner     lists     sets     dictionaries

List Initia­liz­ation

Empty
list_name = []
Non Empty
list_name = [value, ...]

Tuple/Set Initia­liz­ation

Tuples
tuple_name = (value, value, ...)
Sets
set_name = {value, value, ...}

Dictionary Initia­liz­ation

Empty
dict_name = {}
Non Empty
dict_name = {key: value, ...}

MISC

Sort
list_n­ame.sort()
Reverse Sort
list_n­ame.re­verse()
Length
len(li­st/­set­/di­ct/­tuple)
List of Tuples
dict_n­ame.it­ems()
List of Dict Keys
dict_n­ame.keys()
List of Dict Values
dict_n­ame.va­lues()
Count
tuple_­nam­e.c­oun­t(v­alue)
Diff of One Set
set_1.d­if­fer­enc­e(s­et_2)
Diff of Two Sets
set_1.u­ni­on(­set_2)
Common Values
set_1.i­nt­ers­ect­ion­(set_2)
 

Add/Ch­ang­e/A­ccess Lists

Add By Index
list_n­ame.in­ser­t[i­ndex, value]
Add To End
list_n­ame.ap­pen­d(v­alue)
Add Multiple
list_n­ame.ex­ten­d(i­tems)
Get By Index
item = list_n­ame­[index]
Get Index
num = list_2.in­dex­(value)
Change
list_n­ame­[index] = value
Copy
list_2 = list_n­ame.copy()

Add/Ch­ang­e/A­ccess Dictio­naries

Add
dict_1­[key] = value
Copy
dict_2 = dict_1.copy()
Combine
dict_1.up­dat­e(d­ict_2)
Change
dict_1­[ex­isting key] = value
Value of Key
dict_1.ge­t(key)

Add/Ch­ang­e/A­ccess Tuples­/Sets

Add
set_na­me.a­dd­(value)
Add Multiple
set_na­me.u­pd­ate­([v­alue, value, ...])
Index of Value
tuple_­nam­e.i­nde­x(v­alue)
Get by Index
tuple_­nam­e[i­ndex]
 

Delete Collection

Lists
del list_name
Sets
del set_name
Tuples
del tuple_name
Dictio­naries
del dictio­nar­y_name

Delete By Index

Lists
list_n­ame.po­p(i­ndex)
 
del list_n­ame­[index]

Delete By Name

Lists
list_n­ame.re­mov­e(v­alue)
Sets
set_na­me.r­em­ove­(value)
 
set_na­me.d­is­car­d(v­alue)
Dictio­naries
dict_n­ame.po­p(key)
 
del dict_n­ame­[key]

Delete Contents

Lists
del list_n­ame[:]
Dictio­naries
dict_n­ame.cl­ear()
Sets
set_na­me.c­lear()

Download the Collection Data Types Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/crystinvw_collection-data-types.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by crystinvw