[ , ]
ordered by index
changable
not unique
( , )
ordered by index
totally immutable
not unique
{ , }
unordered without index
add, remove, but no change
unique
{ : , : }
ordered by key
changable
unique
list_items = [ "value0" , "value1" , "value2" ]
list allow dublicate values
lists are indexed i.e. ordered
lists values can be changed
tuples = ( "value0" , "value1" , "value2" )
tuples allow dublicate values
tuples are indexed i.e. ordered
tuples are immutable
set_items = { "valueY" , "valueX" , "valueZ" }
set_items = { "valueX" , } sic!
sets are always unique
sets are not indexed i.e. unordered
sets are immutable (items, however, can be deleted, and added)
single sets end with a comma
dictionary_items = { "keyA" : "valueA" , "keyB" : "valueB" }
dictionaries are always unique
a dictionary's items are key-value-pairs
dictionaries are not indexed, but ordered (the key works like a named index)
help(list) shows methods etc.
long_list = short_list_1 + short_list_2
sum(my_list)
min(my_list)
max(my_list)
len(my_list)
my_list = [] #creates an empty list of
my_list[n] = new_value #replaces the item on position n on the list with a new value
my_list.extend(["A","B"]) #add the elements A and B to my_list
https://www.w3schools.com/python/python_lists_methods.asp
.append(value) #adds a value to the list as an additional item
.count() #counts elements in a list
.count("e") #counts elements "e" in the list's elements
.extend() #combines two lists
.index() #gives the index number of an element in a list, or the position of a character in a string
.index(value) #gvie the index of the value
.insert(n,new_value) #adds a new value to the list at position n
.pop() #deletes the last item from the list
.pop(n) #deletes the last item at position n from the list
.pop("name") #detes the item "name" from the list.reverse() #reverses a list
.sort() #sorts a list, and stores it as itself
sorted() #sorts a set and returns a list
.unique() #makes a list unique distinct list of values
tuple() #converts to a tuple
tuple unpacking is also called tuple expansion
var1, var2 = my_tuple(0) #assign each tuple pair of the first tuple to a variable
for var1, var2 in my_tuples:
print(var1)
print(var2)
https://www.w3schools.com/python/python_tuples_methods.asp
.items #gives the keys and values as tuples in a list
.keys #give the keys of a dictionary, not the values
.values #gives the values of a dictionary, not the keys
var = set()
set() #can cast a list to a set
sorted() #sorts a set and returns a list
search works very fast on sets
double values will appear only once in the print() of a set
sets are used to sort out duplicates
https://www.w3schools.com/python/python_sets.asp
.add #adds an element to a set, if the element does not exist already in the set
.discard() #removes elements from a set
.difference #shows differnce between a set and a target set
.intersection() #intersection of sets
.pop() #deletes an element from a set
.union() #union of sets
.update() #adds to a set
{ item1 , item2 , … }
{ "key1": value1 , "key2": value2, … }
double keys are not possible, of equal keys only the latest gives the valid value
dictionaries are sometime nested inside other dictionaries to create an efficient data structure
my_dictionary["my_key"] #accesses the value of the key "my_key"
del my_dictionary["my_key"] deletes the key and its value from the dictionary
in #gives True if a key is in a dictionary
my_dictionary["my_key"] = value #adds or changes a dictionary
dicationary can be nested
my_dictionary["my_key"]["my_subkey"] #accesses the value of the subdictionary inside the dictionary
for key, value in my_dictionary.items():
print(key, value)
https://www.w3schools.com/python/python_dictionaries_methods.asp
.get("my_key") #gets a value avoiding error messages
.items #gives the keys and values as tuples in a list
.keys #gives the keys of a dictionary, not the values
.pop("my_key") deletes the key and its value from the dictionary
.update({"my_key": value}) #updates or add to a dictionary
.values #gives the values of a dictionary, not the keys
"my_key" in my_dictionary #is True, if the key is part of the dictionary
print(my_dictionary["my_key"]) #gives the value to "my_key" in my_dictionary
del_value = my_dictionary.pop("my_key") #deletes the key and its value from the dictionary saves the value in a variable
capitals = {'Australia':'Canberra', 'Belgium':'Brussels', 'Finland':'Helsinki'}
for key,val in capitals.items():
print(key,val)
print([squirrel for squirrel in squirrels_by_park["Union Square Park"] if "Cinnamon" in squirrel["primary_fur_color"]])
the slice notations works slightly different as it handles dimensions viz. row and columns of arrays
[start:end] with start included, but end is excluded!
my_list = [0, 1, 2, 3, 4, 5, 6, 7 ,8]
print(len(my_list)) #output 9
print(my_list) #output [0, 1, 2, 3, 4, 5, 6, 7 ,8]
print(my_list[:]) #output [0, 1, 2, 3, 4, 5, 6, 7 ,8]
print(my_list[:2]) #output [0, 1]
print(my_list[6]) #output 6
print(my_list[6:]) #output [6, 7 ,8]
print(my_list[2:6]) #output [2, 3, 4, 5]
print(my_list[6:2]) #output []
print(my_list[-1]) #output 8
print(my_list[-1:6]) #output []
print(my_list[3:-2]) #output [3, 4, 5, 6]
my_list = [0, 1, 2, 3, 4, 5, 6, 7 ,8]
print(len(my_list)) #output 9
print(my_list[::2]) #output [0, 2, 4, 6, 8]
print(my_list[1::3]) #output [1, 4, 7]
print(my_list[1:7:3]) #output [1, 4]
the collections module holds more advances data containers viz. collections