© 2004-21

Using a 1D List

Think of a list like a set of boxes joined together.
On the front of the box is a number so that you can identify what is in the box by referring to its number.
You can put anything in the box and find the contents by referring to its number.

The code snippet demonstrates 3 ways to create a 1d list

# Author: Your name here
# Date:
# Purpose: Three Ways to initialise a list in python

print("3 Different Ways to Create a List\n")
print("\n1. Initialise an empty list:")
myList1 = []
print ("Contents of myList1:\n{}".format(myList1))
print("\n2. Set each item individually:")
myList2 = ["!","!","!","!","!","!","!","!","!","!"]
print ("Contents of myList1:\n{}".format(myList2))
print("\n3. Use a list comprehension:")
myList3 = ["~" for x in range(10)]

print ("Contents of myList2:\n{}".format(myList3))