Knowing how to manipulate a Python List is where you can really delve into data science. A Python list has square brackets. It is a collection wherein we assign multiple values to one variable. It is important to know how to find certain values within your lists.
Lists do not have to be of a single value type. However, converting a list to a numpy array will coerce the list to a single data type. A Python List should be converted to a numpy array if, for example, you want to make a scatter plot.
Lists can exist inside of a list.
You can look up values in a list, similar to how you lookup values in a string.
Remember the index operator from the lesson about Python strings?
>>> print colors
Do not forget index values start at zero. That is why the above example returns ‘green’.
However, while strings are immutable, lists are mutable. This is a great feature of lists.
>>> lucky_numbers = 36
>>> print lucky_numbers
[3, 21, 7, 68, 36]
See! The fourth index value of the list was changed.
You can use ‘len’ to know the length of a list.
You can use a range function.
[0, 1, 2, 3, 4]
Now you know that lucky_numbers has a range of five values. You might want to loop through the list, while keeping track of the range value.
number = lucky_numbers[i]
You can concatenate lists with the ‘+’ operator.
You can slice a Python list. Again, this is similar to strings.
[3, 21, 7]
There are many Python list methods that are built-in functions to do useful things to your list.
For example, you can append to a list.
>>> print things
Find if something is, or is not in a list.
>>> 7 not in things
The ‘sort’ method will force a list to sort itself, like alphabetically for example.
There are lots great methods for lists of numbers, such as max, min, and sum.
You can loop through an input of numbers, and build a list.
A very powerful method is ‘split’. This allows us to split a string into a list of words.
>>> words = lyric.split()
>>> print words
[‘three’, ‘little’, ‘birds’]
Split sees many spaces as just one space. So, if a line has lots of space at the end, then split will discard all that extra spice. This is very convenient.
Data could consist of no spaces, where every string is delimited by a comma, for example. You would pass the comma in as an argument to split.
>>> jiblist = jibberish.split(‘,’)
>>> print jiblist
[‘heh’, ‘reh’, ‘vtv’]