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', 'Ω']
+ 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.
append() method adds a single item to the end of the list. (Now we have four different datatypes in the list!)
extend() method takes one argument, a list, and appends each of the items of the argument to the original list.
-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].
+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 theshift()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
+pop() list method removes the last item in the list and returns the value it removed.
pop() method. It will remove that item, shift all the items after it to “fill the gap,” and return the value it removed.
-pop() on an empty list raises an exception.
+pop() on an empty list raises an exception.
++☞Calling the
pop()list method without an argument is like thepop()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 toa_list.pop(0). +
You can also use a list in a boolean context, such as an if statement.