Summary of ways to create a list

See Lists may be constructed in several ways.

  1. With [square brackets].
    1. List the elements in square brackets, separated by commas. See List of numbers, List of strings.
      months = [
          "January",  #0
          "February", #1
          "March"     #2
      ]
      
    2. Multiple copies of the same value. See the multiplication operator and often haunts.
      myList = 5 * [0]
      
      for i in myList:
          print(i)
      
      0
      0
      0
      0
      0
      
    3. Start with an empty list, and add additional items with append or insert. See CSV.
      myList = []    #Start with an empty list.
      
      for var in something:
          myList.append(anAdditionalItem)
      
    4. Use a list comprehension.
  2. Concatenate two existing lists.
    bigList = [10, 20, 30] + [40, 50, 60]
    bigList += [70, 80, 90]
    print(bigList)
    
    [10, 20, 30, 40, 50, 60, 70, 80, 90]
    
  3. Split a string into a list of strings.
    1. Split a string into substrings using a separator string. See Split and splitlines.
      preamble = """\
      We hold these truths to be self-evident,
      that all men are created equal,
      that they are endowed by their Creator with certain unalienable Rights,
      that among these are Life, Liberty, and the pursuit of Happiness.
      """
      
      phrases = preamble.split(",\n") #phrases is a list of 4 strings.
      
    2. Split a string into individual characters with list. See Split.
      fdr = "Franklin Delano Roosevelt"
      characters = list(fdr)   #characters is a list of 25 strings, each of length 1
      
    3. Split one string into comma separated values. myString is a string containing comma separated values, and [myString] is a list containing one string. Since [myString] contains only one item, we call next only one time. myString could come from a text file; see CSV.
      import csv
      
      reader = csv.reader([myString]) #[myString] is a list containing one string
      fields = next(reader)           #fields is a list of strings.
      

  4. Read the lines of a text file into a list of strings.
    from disk from web
    Read the file
    into one big
    string of characters
    or one big sequence of bytes;
    then split it into lines
    using splitlines.
    import sys
    
    filename = "/Users/etc."
    
    try:
        infile = open(filename)
    except FileNotFoundError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    except PermissionError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    bigString = infile.read()      #bigString is one big string.
    infile.close()
    
    lines = bigString.splitlines() #lines is a list of strings
    for line in lines:             #line is a string
        print(line)                #line does not end with a newline
    
    import sys
    import urllib.request
    
    url = "http://etc."
    
    try:
        infile = urllib.request.urlopen(url)
    except urllib.error.URLError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    bigSequenceOfBytes = infile.read()
    infile.close()
    
    try:
        #Convert sequence of bytes to string of characters.
        bigStringOfCharacters = bigSequenceOfBytes.decode("utf-8")
    except UnicodeError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    #lines is a list of strings
    lines = bigStringOfCharacters.splitlines()
    for line in lines:
        print(line)             #line does not end with a newline
    
    Read the file
    into a list of lines
    using readlines.
    import sys
    
    filename = "/Users/etc."
    
    try:
        infile = open(filename)
    except FileNotFoundError as error:
        print(error, fie = sys.stderr)
        sys.exit(1)
    except PermissionError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    lines = infile.readlines() #lines is a list of strings
    infile.close()
    
    for line in lines:
        #end="" because each line already ends with a newline
        print(line, end = "")
    
    import sys
    import urllib.request
    
    url = "http://etc."
    
    try:
        infile = urllib.request.urlopen(url)
    except urllib.error.URLError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    #lines is a list of sequences of bytes.
    lines = infile.readlines()
    infile.close()
    
    try:
        #lines is now a list of strings of characters.
        lines = [line.decode("utf-8") for line in lines]
    except UnicodeError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    for line in lines:
        #end="" because each line already ends with a newline
        print(line, end = "")