The Python List – Delve Into Data Science

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?

>>> colors = [‘blue’, ‘green’, ‘red’] >>> print colors[1] green

 

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 = [3, 21, 7, 68, 93] >>> lucky_numbers[4] = 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.

>>> print len(lucky_numbers)
5

 

You can use a range function.

>>> print range(len(lucky_numbers))
[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.

for i in range(len(lucky_numbers)):
++++number = lucky_numbers[i] ++++print number

 

You can concatenate lists with the ‘+’ operator.

You can slice a Python list. Again, this is similar to strings.

>>> print lucky_numbers[0:3] [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.

>>> things = list()
>>> things.append(‘food’)
>>> things.append(5)
>>> print things
[‘food’, 5]

 

Find if something is, or is not in a list.

>>> ‘book’ in things
False
>>> 7 not in things
True

 

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 list building example.
How to loop through user input, and build a list with the input.

A very powerful method is ‘split’. This allows us to split a string into a list of words.

>>> lyric = ‘three little birds’
>>> 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.

>>> jibberish = ‘heh,reh,vtv’
>>> jiblist = jibberish.split(‘,’)
>>> print jiblist
[‘heh’, ‘reh’, ‘vtv’]

 

Leave a Reply

Your email address will not be published. Required fields are marked *