06.0 Python‎ > ‎Python Notes‎ > ‎

List Processing

In python:

  • lists are dynamic - there size in not fix and elements can be appended to the end of the list very easily. Use .append() to add an element to the end of a list
  • each list can contain different data types, including other lists and objects
  • strings are a list of characters
Python Docs: More on List Objects 

Programming Bits

Create an empty list:
    my_list = []

Add an element to a list:
    my_list.append("some string")
this list will now have two elements my_list[0] will contain "some string" and my_list[1] will contain the integer value of 7

Length of a list:  find out the number of elements in a list -- just like finding the length of a string

Insert an element at a given position in the list:
    a_list = ["some words", "in a","simple", "list" ]
    a_list.index(1,"and strings")
a_list will now contain: ["some words","and strings", "in a","simple", "list" ]