{article Dive into Python}{title} {text}{/article}

Defining Lists Negative List Indices Slicing a List Slicing Shorthand
Adding Elements to Lists The Difference between extend and append Searching Lists Removing Elements from a Lists
List Operators

In Python, the closest object to an array is a list,

Lists in Python are created by comma-separated values, between square brackets.

List items do not need to have all the same data type. Unlike strings which are immutable, it is possible to change individual elements of
a list (lists are mutable):

immutable object is an object whose state cannot be modified after it is created

mutable object, which can be modified after it is created.


Defining Lists

  1. li = ["a", "b", "mpilgrim", "z", "example"]
  2. li[0]
  3. li[4]
  1. First, you define a list of five elements.
  2. A list can be used like a zero−based array. The first element of any non−empty list is always li[0].
  3. The last element of this five−element list is li[4], because lists are always zero−based.

{source}
<!-- You can place html anywhere within the source tags -->

<!-- <link type="text/css" rel="Stylesheet" href="/syntaxhighlighter_3.0.83/styles/shThemeDefault.css"/> -->


<pre class="brush:py;">


Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> li = ["a", "b", "mpilgrim", "z", "example"] #First, you define a list of five elements.
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[0] # A list can be used like a zero−based array. The first element of any non−empty list is always li[0].
'a'
>>> li[4] # The last element of this five−element list is li[4], because lists are always zero−based.
'example'
>>>



</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>

 


{/source}


Negative List Indices

  1. li[-1]
  2. li[-3]

  1. A negative index accesses elements from the end of the list counting backwards. The last element of
    any non−empty list is always li[−1].
  2. If the negative index is confusing to you, think of it this way: li[−n] == li[len(li) − n]. So
    in this list, li[−3] == li[5 − 3] == li[2].

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">


>>> li = ["a", "b", "mpilgrim", "z", "example"] # First, you define a list of five elements.
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[-1] # A negative index accesses elements from the end of the list counting backwards. The last element of any non−empty list is always li[−1].
'example'
>>> li[-3] # If the negative index is confusing to you, think of it this way: li[−n] == li[len(li) − n]. So in this list, li[−3] == li[5 − 3] == li[2].
'mpilgrim'
>>>



</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}


Slicing a List

li

  1. li[1:3]
  2. li[1:-1]
  3. li[0:3]
  1. You can get a subset of a list, called a "slice", by specifying two indices. The return value is a new list
    containing all the elements of the list, in order, starting with the first slice index (in this case li[1]), up to but
    not including the second slice index (in this case li[3]).

  2. Slicing works if one or both of the slice indices is negative. If it helps, you can think of it this way: reading the
    list from left to right, the first slice index specifies the first element you want, and the second slice index
    specifies the first element you don't want. The return value is everything in between.

  3. Lists are zero−based, so li[0:3] returns the first three elements of the list, starting at li[0], up to but not
    including li[3].


Slicing Shorthand

>>> li = ["a", "b", "mpilgrim", "z", "example"]
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
1.>>> li[:3]
['a', 'b', 'mpilgrim']

2. 3.>>> li[3:]
['z', 'example']


4. >>> li[:]

['a', 'b', 'mpilgrim', 'z', 'example']
>>>

  1. If the left slice index is 0, you can leave it out, and 0 is implied. So li[:3] is the same as li[0:3] from
    Example 3.8, Slicing a List.

  2. Similarly, if the right slice index is the length of the list, you can leave it out. So li[3:] is the same as
    li[3:5], because this list has five elements.
  3. Note the symmetry here. In this five−element list, li[:3] returns the first 3 elements, and li[3:] returns
    the last two elements. In fact, li[:n] will always return the first n elements, and li[n:] will return the rest,
    regardless of the length of the list.

  4. If both slice indices are left out, all elements of the list are included. But this is not the same as the original li
    list; it is a new list that happens to have all the same elements. li[:] is shorthand for making a complete copy
    of a list.


Adding Elements to Lists

>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
1. >>> li.append("new")
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
2. >>> li.insert(2, "new")
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
3. >>> li.extend(["two", "elements"])
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']

  1. append adds a single element to the end of the list.

  2. insert inserts a single element into a list. The numeric argument is the index of the first element that gets
    bumped out of position. Note that list elements do not need to be unique; there are now two separate elements with the value 'new', li[2] and li[6].

  3. extend concatenates lists. Note that you do not call extend with multiple arguments; you call it with one
    argument, a list. In this case, that list has two elements.


The Difference between extend and append

>>> li = ['a', 'b', 'c']
>>> li
['a', 'b', 'c']
1.>>> li.extend(['d', 'e', 'f'])
>>> li
['a', 'b', 'c', 'd', 'e', 'f']
2.>>> len(li)
6
>>> li[-1]
'f'
>>> li = ['a', 'b', 'c']
3.>>> li.append(['d', 'e', 'f'])
>>> li
['a', 'b', 'c', ['d', 'e', 'f']]
4.>>> len(li)
4
>>> li[-1]
['d', 'e', 'f']

  1. Lists have two methods, extend and append, that look like they do the same thing, but are in fact
    completely different. extend takes a single argument, which is always a list, and adds each of the elements of that list to the original list.
  2. Here you started with a list of three elements ('a', 'b', and 'c'), and you extended the list with a list of another three elements ('d', 'e', and 'f'), so you now have a list of six elements
  3. On the other hand, append takes one argument, which can be any data type, and simply adds it to the
    end of the list. Here, you're calling the append method with a single argument, which is a list of three elements.
  4. Now the original list, which started as a list of three elements, contains four elements. Why four? Because the last element that you just appended is itself a list. Lists can contain any type of data, including other lists. That may be what you want, or maybe not. Don't use append if you mean extend.

Searching Lists

li = ["a", "b", "new", "mpilgrim", "z", "example", "new", "two", "elements"]

  1. li.index("example")
  2. li.index("new")
  3. li.index("c")
  4. "c" in li

  1. index finds the first occurrence of a value in the list and returns the index.
  2. index finds the first occurrence of a value in the list. In this case, 'new' occurs twice in the list, in li[2] and li[6], but index will return only the first index, 2.
  3. If the value is not found in the list, Python raises an exception. This is notably different from most languages, which will return some invalid index. While this may seem annoying, it is a good thing, because it means your program will crash at the source of the problem, rather than later on when you try to use the invalid index.
  4. To test whether a value is in the list, use in, which returns True if the value is found or False if it is not.



Removing Elements from a List

  1. li.remove("z")
  2. li.remove("new")
  3. li.remove("c")
  4. li.pop()

  1. remove removes the first occurrence of a value(z) from a list.
  2. remove removes only the first occurrence of a value. In this case, 'new' appeared twice in the list, but li.remove("new") removed only the first occurrence.
  3. If the value is not found in the list, Python raises an exception. This mirrors the behavior of the index method.
  4. pop is an interesting beast. It does two things: it removes the last element of the list, and it returns the value that it removed. Note that this is different from li[−1], which returns a value but does not change the list, and different from li.remove(value), which changes the list but does not return a value.


List Operators

li = ['a', 'b', 'mpilgrim']

  1. li = li + ['example', 'new']
  2. li += ['two']
  3. li = [1, 2] * 3

  1. Lists can also be concatenated with the + operator. list = list + otherlist has the
    same result as list.extend(otherlist). But the + operator returns a new (concatenated)
    list as a value, whereas extend only alters an existing list. This means that extend is faster,
    especially for large lists.

  2. Python supports the += operator. li += ['two'] is equivalent to li.extend(['two']).
    The += operator works for lists, strings, and integers, and it can be overloaded to work for
    user−defined classes as well. (More on classes in Chapter 5.).

  3. The * operator works on lists as a repeater. li = [1, 2] * 3 is equivalent to li = [1,
    2] + [1, 2] + [1, 2], which concatenates the three lists into one.