Picking up from 'The Noobs of Python Ep.2 List' we start to delve deeper into List with Slice, Loops & more.
This thread has been updated to reflect a more concise direction with this and future additions to the Ep.2 series
Q: What are Slices?
A: Slicing is used to fetch multiple items from a sequence. Slices are written using the same notation as an index, but this time with two or three integers separated by colons. The first value is the inclusive starting point and the second number is the exclusive end point of the slice. ex:
sith = ['Lord', 'Emperor', 'Darth', 'Count']
sith[1:3] will return ['Emperor', 'Darth']
Note : The first thing you will notice is that indexing is zero-based, that means you start counting at 0.
So sith[0:2]
means that the slice will start from index 0 and stop just before index 2,
(i.e., fetch items at positions 0 and 1). Here are some examples below:
|------|----------|---------|-------|
['Lord', 'Emperor', 'Darth', 'Count']
|------|----------|---------|-------|
0 1 2 3
Doing sith[0:1]
will return'Lord'
since it is 0 and ends before 1:
>>> sith[0:1]
['Lord']
The syntax for slice is as follows:
sith[start:end] # items start through end-1
sith[start:] # items start through the rest of the array
sith[:end] # items from the beginning through end-1
sith[:] # a copy of the whole array
There is also the step value, which can be used with any of the above:
sith[start:end:step] # start through not past end, by step
>>> sith[0:3:2]
['Lord', 'Darth']
>>>
Q: Wait a minute? You never mentioned loops before?! How does that work?
A: Technically, a for loop repeats the code block once for each value in a
list or list-like value, so picture this:
for i in range(9):
print(i)
1 2 3 4 5 6 7 8
This is because the return value from range(9)
is a list-like value that
Python considers similar to [0, 1, 2, 3, 4, 5, 6, 7, 8]
which is why I thought it
would be good to package the explanations. Below we will do some string concatenation and using the list jedi
and we'll run a for loop to iterate over indexes of a list. You can do this with your favorite text editor and saving the file as jedi.py or just running it in idle3
jedi = ['Luke', 'Obi Wan', 'Ezra', 'Kanan']
for x in range(len(jedi)):
print('Index ' + str(x) + ' in Jedi ' + jedi[x])
this returns :
Index 0 in Jedi Luke
Index 1 in Jedi Obi Wan
Index 2 in Jedi Ezra
Index 3 in Jedi Kanan
PRO TIP: We can also use a for
loop to create something interesting with List... Taking a peak into List comprehension. Look at the example below:
>>> A = [None] * 3
>>> for i in range(3):
A[i] = [None] * 3
>>> A
[[None, None, None], [None, None, None], [None, None, None]]
>>>
This is called a Multidimensional list, this is a form of nesting and some people are familiar with this from other languages like C called arrays. We used the for loop to create an empty List with 3 list within, each with 3 indexes within it. If you have questions, Post them in the comments section!
Below we will populate this empty multidimensioanl list in a short example to get an understanding of the lists within a list:
>>> A[0][1] = str('Spiderman')
>>> A
[[None, 'Spiderman', None], [None, None, None], [None, None, None]]
>>> A[0][0] = str('Peter Parker')
>>> A
[['Peter Parker', 'Spiderman', None], [None, None, None],
[None, None, None]]
>>> A[0][2] = str('Avengers')
>>> A
[['Peter Parker', 'Spiderman', 'Avengers'], [None, None, None],
[None, None, None]]
>>>
In that example we populate List [0] and it's indexes [x]. We can keep this going by populating the rest of the list and even use the method .append()
to add a list within this list as follows:
A.append(['Reed Richards', 'Mr. Fantastic', 'Fantastic Four'])
>>> A
[['Peter Parker', 'Spiderman', 'Avengers'], [None, None, None],
[None, None, None], ['Reed Richards', 'Mr. Fantastic', 'Fantastic Four']]
>>>
Post your code and combine what you have learn to see what crazy stuff you come up with ! If you have questions just ask ! Happy Coding Code_Warriors