diff --git a/native-datatypes.html b/native-datatypes.html index 971624f..b6251a1 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -277,19 +277,25 @@ ZeroDivisionError: Fraction(0, 0) >>> a_list.append(True) >>> a_list ['a', 2.0, 3, True] ->>> a_list.extend(['four', 'e']) +>>> a_list.extend(['four', 'Ω']) >>> a_list -['a', 2.0, 3, True, 'four', 'e'] ->>> a_list.insert(1, 'a') +['a', 2.0, 3, True, 'four', 'Ω'] +>>> a_list.insert(0, 'Ω') >>> a_list -['a', 'a', 2.0, 3, True, 'four', 'e'] +['Ω', 'a', 2.0, 3, True, 'four', 'Ω']
  1. The + operator concatenates lists. A list can contain any number of items; there is no size limit (other than available memory). A list can contain items of any datatype; they don’t all need to be the same type. Here we have a list containing a string, a floating point number, and an integer.
  2. The append() method adds a single item to the end of the list. (Now we have four different datatypes in the list!)
  3. Lists are implemented as classes. “Creating” a list is really instantiating a class. As such, a list has methods that operate on it. The extend() method takes one argument, a list, and appends each of the items of the argument to the original list. -
  4. The insert() method inserts a single item into a list. The first argument is the index of the first item in the list that will get bumped out of position. List items do not need to be unique; for example, there are now two separate items with the value 'a', a_list[0] and a_list[1]. +
  5. The insert() method inserts a single item into a list. The first argument is the index of the first item in the list that will get bumped out of position. List items do not need to be unique; for example, there are now two separate items with the value 'Ω': the first item, a_list[0], and the last item, a_list[6].
+ +
+

a_list.insert(0, value) is like the shift() function in Perl. It adds an item to the beginning of the list, and all the other items have their positional index bumped up to make room. +

+

Let’s look closer at the difference between append() and extend(). +

 >>> a_list = ['a', 'b', 'c']
 >>> a_list.extend(['d', 'e', 'f'])  
@@ -378,7 +384,7 @@ ValueError: list.remove(x): x not in list

Another interesting list method is pop(). The pop() method is yet another way to remove items from a list, but with a twist.

->>> a_list = ['a', 'b', 'new', 'mpilgrim']
+>>> a_list = ['a', 'b', 'new', 'mpilgrim']
 >>> a_list.pop()   
 'mpilgrim'
 >>> a_list
@@ -395,11 +401,16 @@ ValueError: list.remove(x): x not in list
Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop from empty list +
  1. When called without arguments, the pop() list method removes the last item in the list and returns the value it removed.
  2. You can pop arbitrary items from a list. Just pass a positional index to the pop() method. It will remove that item, shift all the items after it to “fill the gap,” and return the value it removed. -
  3. All around the mulberry bush… calling pop() on an empty list raises an exception. +
  4. Calling pop() on an empty list raises an exception.
+
+

Calling the pop() list method without an argument is like the pop() function in Perl. It removes the last item from the list and returns the value of the removed item. Perl has another function, shift(), which removes the first item and returns its value; in Python, this is equivalent to a_list.pop(0). +

+

Lists In A Boolean Context

You can also use a list in a boolean context, such as an if statement.