Python Lists

Python Lists

Python Lists

Python knows many types of compound data, which are used to concatenate other values simultaneously. The most versatile is the python list, which can be written as a comma-separated list of values (objects) between square brackets. Python Lists can contain different types of items, but typically, items are all types.


    >>> squares = [1, 4, 9, 16, 25]
    >>> squares
    [1, 4, 9, 16, 25]

    Like strings (and all other underlying sequence types), lists can be indexed and truncated:

      >>> squares[0]  # indexing returns the item
      1
      >>> squares[-1]
      25
      >>> squares[-3:]  # slicing returns a new python list
      [9, 16, 25]
      

      All slice operations return a new python list containing the requested elements. This means that the following fragment returns a new (shallow) copy of the list:

        >>> squares[:]
        [1, 4, 9, 16, 25]
        

        The python lists also support operations like concatenation:

          >>> squares + [36, 49, 64, 81, 100]
          [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

          Unlike strings, which are immutable, lists are a variable type, meaning it is possible to change their contents:

            >>> cubes = [1, 8, 27, 65, 125]  # something's wrong here
            >>> 4 ** 3  # the cube of 4 is 64, not 65!
            64
            >>> cubes[3] = 64  # replace the wrong value
            >>> cubes
            [1, 8, 27, 64, 125]
            

            You can also add new items to the end of the python list using the append () method (we'll see more about the later methods):

              >>> cubes.append(216)  # add the cube of 6
              >>> cubes.append(7 ** 3)  # and the cube of 7
              >>> cubes
              [1, 8, 27, 64, 125, 216, 343]
              

              It is also possible to assign slices, and this can also change the size of the list or make it completely clean:

                >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
                >>> letters
                ['a', 'b', 'c', 'd', 'e', 'f', 'g']
                >>> # replace some values
                >>> letters[2:5] = ['C', 'D', 'E']
                >>> letters
                ['a', 'b', 'C', 'D', 'E', 'f', 'g']
                >>> # now remove them
                >>> letters[2:5] = []
                >>> letters
                ['a', 'b', 'f', 'g']
                >>> # clear the python list by replacing all the elements with an empty list
                >>> letters[:] = []
                >>> letters
                []


                The built-in function len() also applies to lists to calculate the length of the list:

                  >>> letters = ['a', 'b', 'c', 'd']
                  >>> len(letters)
                  4

                  For example, it is possible to create python lists (create lists with other lists):

                    >>> a = ['a', 'b', 'c']
                    >>> n = [1, 2, 3]
                    >>> x = [a, n]
                    >>> x
                    [['a', 'b', 'c'], [1, 2, 3]]
                    >>> x[0]
                    ['a', 'b', 'c']
                    >>> x[0][1]
                    'b'
                    





































                    Python Lists Python Lists Reviewed by Sk Jha on October 09, 2019 Rating: 5

                    No comments:

                    Powered by Blogger.