How to Create Lists in Python
As in real life, before you can do anything with a list, you must create it. Python lists can mix types. However, it’s always a best practice to restrict a list to a single type when you can. The following steps demonstrate how to create Python lists.
1Open a Python Shell window.
You see the familiar Python prompt.
2Type List1 = [One, 1, Two, True] and press Enter.
Python creates a list named List1 for you. This list contains two string values (One and Two), an integer value (1), and a Boolean value (True). Of course, you can’t actually see anything because Python processes the command without saying anything.
Notice that each data type that you type is a different color. When you use the default color scheme, Python displays strings in green, numbers in black, and Boolean values in orange. The color of an entry is a cue that tells you whether you have typed the entry correctly, which helps reduce errors when creating a list.
3Type print(List1) and press Enter.
You see the content of the list as a whole. Notice that the string entries appear in single quotes, even though you typed them using double quotes. Strings can appear in either single quotes or double quotes in Python.
4Type dir(List1) and press Enter.
Python displays a list of actions that you can perform using lists. Notice that the output is actually a list. So, you’re using a list to determine what you can do with another list.
As you start working with objects of greater complexity, you need to remember that the dir() command always shows what tasks you can perform using that object. The actions that appear without underscores are the main actions that you can perform using a list. These actions are the following:
5Close the Python Shell window.
You now have a list.