list in python


  • You can perform all the standard sequence operations on lists, such as indexing, slicing, concatenating, and multiplying. But the interesting thing about lists is that they can be modified.
  • In this section, you see some of the ways you can change a list: item assignments, item deletion, slice assignments, and list methods. (Note that not all list methods actually change their list.)
  • Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in list are called elements or sometimes items.
  • Lists are one of the most versatile object types in Python.
  • A list is an ordered collection of objects. It is represented by elements separated by commas and enclosed between square brackets.
  • A list that contains no elements is called an empty list; you can create one with empty brackets, [ ].

The list Function

Because strings can’t be modified in the same way as lists, sometimes it can be useful to create a list from a string. You can do this with the list function:

    >>> list('Hello')
    ['H', 'e', 'l', 'l', 'o']
  • Note that list works with all kinds of sequences, not just strings.

List Initialization

If you know in advance that a list is going to have five elements, we can initialize it with a default value:

    >>> codons = [None] * 5
    >>> codons
    [None, None, None, None, None]
  • This type of list initialization can be useful when working with big lists and the number of elements is known beforehand.
  • Defining a list with a fixed size is more efficient than creating an empty list expanding it as needed
  • Fixed sized lists don’t have the overhead of lists that change positions in memory.

Accessing List Elements

As one of the other sequence data types, you access list elements by an index starting at zero.

    >>> first_list=[1,2,3,4,5]
    >>> first_list[0]
    1
    >>> first_list[1]
    2

List Methods

  • You’ve encountered functions already, but now it’s time to meet a close relative: methods.
  • A method is a function that is tightly coupled to some object, be it a list, a number, a string, or whatever. In general, a method is called like this:
  • object.method(arguments)
  • Lists have several methods that allow you to examine or modify their contents.
    • append
    • count
    • extend
    • index
    • insert
    • pop
    • remove
    • reverse
    • sort

append

  • The append method is used to append an object to the end of a list:
  • >>> lst = [1, 2, 3]
    >>> lst.append(4)
    >>> lst
    [1, 2, 3, 4]

count

  • The count method counts the occurrences of an element in a list:
  • >>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')
    2
    >>> x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
    >>> x.count(1)
    2
    >>> x.count([1, 2])
    1

extend

  • The extend method allows you to append several values at once by supplying a sequence of the values you want to append. In other words, your original list has been extended by the other one:
  • >>> a = [1, 2, 3]
    >>> b = [4, 5, 6]
    >>> a.extend(b)
    >>> a
    [1, 2, 3, 4, 5, 6]

index

  • The index method is used for searching lists to find the index of the first occurrence of a value:
  • >>> knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni']
    >>> knights.index('who')
    4

insert

  • The insert method is used to insert an object into a list:
  • >>> numbers = [1, 2, 3, 5, 6, 7]
    >>> numbers.insert(3, 'four')
    >>> numbers
    [1, 2, 3, 'four', 5, 6, 7]

pop

  • The pop method removes an element (by default, the last one) from the list and returns it:
  • >>> x = [1, 2, 3]
    >>> x.pop()
    3
    >>> x
    [1, 2]
    >>> x.pop(0)
    1
    >>> x
    [2]

reverse

  • The reverse method reverses the elements in the list.
  • >>> x = [1, 2, 3]
    >>> x.reverse()
    >>> x
    [3, 2, 1]