In [1]:
l = [] # Can create an empty list
l = ["a","b","c","d","e","f"] # Defining a simple list of strings
print(l) # Like all Python objects you can print them directly for information
print(l[0]) # Accessing an index directly uses '[i]' notation
print(l[4])
In [2]:
l.append("g") # The append member function (or 'method') let's you add an element
# to the end of the list.
print(l)
append()
method is called from the list variable, it is not an outside function taking a list as an argument like the print function.append()
and extend()
methods.
In [3]:
l = ["a","b","c","d","e","f"]
l.append(["g","h","i","j"]) # Appending a list to a list
print(l)
print()
l = ["a","b","c","d","e","f"]
l.extend(["g","h","i","j"]) # Extending a list
print(l)
In [4]:
l = ["a","c"]
print(l)
l.insert(1,"b") # Puts object at an index and shunts all equal and higher elements upwards
print(l)
# You can query what the index is for a particular value
print(l.index("c"))
# Now what happens when you remove a value at an index
popped = l.pop(2) # the 'pop(2)' actually returns the value of the removed object
print(l)
print(popped)
# Could have used 'pop()' with no argument to remove the last element by default.
# Can also remove by value
l = ["a", "b", "c", "b"]
l.remove("b") # Removes the first instance of the 'b' value, leaves all others
print(l)
In [5]:
l = [["a", "b"], ["c", "d", "e"]] # Nesting lists is easy and doesn't have to be square
print(l[0][1]) # Accessing by index is evaluated left->right
print(l[1][2])
In [6]:
l = ["a", "b", "c", "d", "e"]
print(l[-1])
In [7]:
l = ["a", "b"]
print(l*3) # Creates a new list of repeating elements
print(l + ["c", "d"]) # Creates a new list with elements from second list appended
In [8]:
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l[0:3]) # Slicing uses indices and notation [from : to : step(optional)]
print(l[4:9:2])
print(l[4:]) # Leaving out either side of the colon implies all elements
print(l[5:-2]) # Negative indices still viable
print(l is not l[:]) # Can use slicing to return a copy of the list rather than the object
()
rather than square brackets []
to initialise themselves.
In [9]:
t = () # Can create an empty tuple
t = ("a", "b", "c", "d", "e", "f")
print(t)
print(t[1]) # Access index
print(t[0:2]) # Tuples can also slice to return a new tuple
# t[1] = "k" # Not allowed as tuples are immutable
In [10]:
t = ([1,1],[2,2])
print(t)
t[0][0]=2
print(t)
In [11]:
t = 1,2,3 # Pack integers into a tuple
print(t)
one, two, three = t # Unpack tuple values into new variables.
print(one,two,three)
In [12]:
t = [1,2,3]
# Can also unpack lists/any iterable object, but be careful of unordered dicts/sets!
# Don't need to specify variables for all elements of tuple/list.
# Use the '*' to indicate multiple values placed into one variable
one, *rest = t
print(one,rest)
In [13]:
a, b = 1, 2
a, b = b, a+b # Update, the RHS is fully evaluated before the LHS
print(a,b)
{}
and comma separated key : value pairs.
In [14]:
ages = {} # An empty dictionary
ages = {"David":27, "Andy":28, "Emma":4} # Initialise with keys and values
print(ages["David"]) # Return the value by using the key
ages["Darcey"] = 2 # Can add/change keys
print(ages["Darcey"])
In [15]:
viewKeys = ages.keys() # Similar object with values() method
print(viewKeys)
ages["Rosie"] = 58
print(viewKeys) # View object is updated without having to called keys() again
{}
.
In [16]:
s1 = {"a", "b", "c", "d"} # Initialised
print(s1)
s1.add("e") # Can add and remove elements
s1.remove("e")
s2 = {"c", "d", "e", "f"}
# Several mathematical operations
print(s1.union(s2))
print(s1.difference(s2))
In [17]:
l = [0,1,2,3,4]
t = tuple(l) # Cast iterable to tuple
s = set(l) # Cast iterable to set
d = dict([["a", 1], ["b", 2]]) # Cast iterable of pairs to dictionary
print(d)
name = "Dave"
l = list(name) # Strings are iterable
print(l)
del
keyword.
In [18]:
a = 1
b = [0,1,2,3,4]
c = {"Dave":27, "Andy":28}
del a
#print(a) # This won't work as 'a' no longer exists
del b[2] # Delete by index
print(b)
del b # Delete whole object
del c["Dave"] # Delete by key
print(c)
del c # Delete whole object