Python Collections (Arrays)
Python Collections (Arrays)
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the
specified value
extend() Add the elements of a list (or any
iterable), to
the end of the current list
index() Returns the index of the first element
with
the specified value
List
insert() Adds an element at the specified
position
pop() Removes the element at the
specified
position
remove() Removes the item with the
specified value
reverse() Reverses the order of the list
sort() Sorts the list
Tuple
#NOT a tuple
thistuple = ("apple")
print(type(thistuple)) # class str
Tuple
Tuples are unchangeable, so you cannot remove items
from it, but you can delete the tuple completely:
eg: thistuple = ("apple", "banana",
"cherry")
del thistuple
print(thistuple) #this will raise an
error because the tuple no longer exists
To join two or more tuples you can use the + operator:
eg:tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
It is also possible to use the tuple() constructor to make a
tuple.
eg:thistuple = tuple(("apple", "banana",
"cherry"))
# note the double round-brackets
Tuple Methods
Python has two built-in methods that you can
use on tuples.
Method Description
count() Returns the number of times a
specified value occurs in a
tuple
index() Searches the tuple for a
specified value and returns
the
position of where it was found
Set
Set Methods
Method Description
add() Adds an element to the set
clear() Removes all the elements from the
set
copy() Returns a copy of the set
difference() Returns a set containing the difference
between two or more sets
difference_update() Removes the items in this set that are also included
in another, specified set
discard() Remove the specified item
Set
Method Description
intersection() Returns a set, that is the intersection of two
other sets
intersection_update() Removes the items in this set that are not
present in other, specified set(s)
isdisjoint() Returns whether two sets have a intersection
or
not
issubset() Returns whether another set contains this set
or
not
issuperset() Returns whether this set contains another set
or
not
Set
Method Description
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric
differences of two sets
symmetric_difference_update()
inserts the symmetric differences
from this set and another
union() Return a set containing the union of
sets
update() Update the set with the union of this set
and
others
Dictionary
A dictionary is a collection which is unordered, changeable and
indexed.In Python dictionaries are written with curly brackets,
and they have keys and values.
eg: thisdict
= {"brand": "Ford“,"model": "Mustang“,"year": ”1964”}
You can access the items of a dictionary by referring to its key name,
inside square brackets.
eg: x = thisdict["model"] get the value of the "model" key:
You can change the value of a specific item by referring to its key name.
eg:thisdict["year"] = 2018
You can loop through a dictionary by using a for loop.
# print all key names in the dictionary, one by one
eg:for x in thisdict:
print(x)
# print all values in the dictionary ,one by one
eg:for x in thisdict:
print(thisdict[x])
Dictionary
# use the values() function to return values of a
dictionary
eg:for x in thisdict.values():
print(x)
# Loop through both keys and values, by using
the items() function
eg:for x, y in thisdict.items():
print(x, y)
to determine if a specified key is present in a dictionary use
the in keyword
eg: if "model" in thisdict:
print("Yes, 'model' is one of the keys ")
to determine how many items (key-value pairs) a dictionary
has, use the len() method.
eg:print(len(thisdict))
Dictionary
Adding an item to the dictionary is done by using a new index key
and assigning a value to it
eg:thisdict["color"] = "red“
There are several methods to remove items from a dictionary
# The pop() method removes the item with the specified key
name
eg:thisdict.pop("model")
# The popitem() method removes the last inserted item
eg:thisdict.popitem()
# The del keyword removes the item with the specified key
name
eg: del thisdict["model"]
# The del keyword can also delete the dictionary completely
eg: del thisdict
# The clear() keyword empties the dictionary:
eg: thisdict.clear()
Dictionary
There are ways to make a copy,
#one way is to use the built-in Dictionary
method copy().
eg:mydict = thisdict.copy()
#Another way to make a copy is to use the built-
in method dict().
eg:mydict = dict(thisdict)
A dictionary can also contain many dictionaries, this is called nested
dictionaries.
eg: myfamily = {"child1" : {"name" : "Emil“,"year" : 2004},
"child2" : {"name" : "Tobias“,"year" : 2007},
"child3" : {"name" : "Linus“,"year" : 2011}}
or, to nest three dictionaries that already exists as
child1 = {"name" : "Emil“,"year" : 2004}
child2 = {"name" : "Tobias“,"year" : 2007}
child3 = {"name" : "Linus“,"year" : 2011}
myfamily = {"child1" : child1, "child2" :
child2,"child3" : child3}
Dictionary
It is also possible to use the dict() constructor to make a
new dictionary:
eg :thisdict = dict(brand="Ford", model="Mustang",
year=1964)
*note that keywords are not string literals
*note the use of equals rather than colon for
the assignment
Dictionary Methods
Method Description
clear() Removes all the elements from the
dictionary
copy() Returns a copy of the dictionary
fromkeys() Returns a dictionary with the specified
keys and
values
get() Returns the value of the specified key
items() Returns a list containing the a tuple for
Dictionary
keys() Returns a list containing the dictionary's
keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
setdefault() Returns the value of the specified key. If the