Open Source How to Create and Print Lists in Python

How to Create and Print Lists in Python

Today’s Python lesson will cover the basic concepts of lists. We will learn about the data type, how to add values to it, the rules for doing so, and how to print those values out. Additionally, we will cover how to use For Loops on lists, how to compare two lists using IF and Elif statements, and discuss some miscellaneous details about Pythonic lists in general.

Lists are data types in Python. Whereas a variable can be thought of as a box with a single, solitary item in them – an item that you can take out, replace, or put back in – lists are more like a filing cabinet, capable of holding multiple items.

Here are a few facts about Python lists to keep in mind before we begin:

  • Lists consist of one or more ordered items.
  • Lists can contain objects of any type – even mixed.
  • List elements are accessed by their index or indices.
  • Lists can be nested.
  • Lists are mutable, unlike certain other data types, meaning that the data contained in them can be changed, re-ordered, added, or removed. This differs from immutable data types, whose data can never change.

Create Lists using Python

To create a list in Python, you place your data items in a comma-separated list, enclosed between two square brackets or “[].” Lists are “ordered” in the sense that, once that list is created, the order of the items does not change. Note that does not mean the items are sequential – meaning, you don’t need them to be in an order such as “abcde” or “123456”. Nor does it mean that you can never re-order a list.

Additionally, lists can contain both strings and integer types at the same time.

Here is an example of how to create a list in Python:

# Creating some lists in Python
nameList = ["James","Chad", "Hulk Hogan", "He-Man"];
ageList = [42, 89, 17, 4];
mixedList = ["Pumpkin", 2000, "Steve Austin", 2999];

Printing Elements in a List with Python

We can access the items in our list by referencing their index or indices. Items in a list start at reference 0. So, for instance, to print out the name “James” in the nameList list, you would reference position 0 – not 1, as you might suspect. Here is how to print the values in a list in Python:

# Creating some lists in Python
nameList = ["James","Chad", "Hulk Hogan", "He-Man"];
ageList = [42, 89, 17, 4];
mixedList = ["Pumpkin", 2000, "Steve Austin", 2999];

# Printing the values in lists

# Printing the first value in the list
print("The first item in nameList is: ")
print(nameList[0]);

# Printing the second value in the list
print("The second item in nameList is: ")
print(nameList[1]);

# Printing items in a range or multiple values in a list
print("The second through fourth values in the list are:")
print(nameList[0:3]);

# Printing all of the items in a list with the print function
print("Here are all of the items in nameList:")
print(nameList);

This will result in:

The first item in nameList is: 
James
The second item in nameList is: 
Chad
The second through fourth values in the list are: 
['James', 'Chad', 'Hulk Hogan']
Here are all of the items in nameList: 
['James', 'Chad', 'Hulk Hogan', 'He-Man']

Note that when we print a single item from our list using the print() function, everything looks fine. However, when we print more than one item from the list using the print() function, our results are encased in square brackets and delimited with commas.

There are several ways to overcome this. First, you can use the * operator, like so:

# Creating a list in Python
nameList = ["James","Chad", "Hulk Hogan", "He-Man"];

# Printing all items from a list with *
print(*nameList);

This will print out the list, giving you the following output:

James Chad Hulk Hogan He-Man

We could further format the output by using sep. Here is how we do that Python code:

# Creating a list in Python
nameList = ["James","Chad", "Hulk Hogan", "He-Man"];

# Printing all items from a list with *
print(*nameList);

# Print items in list separated by a comma
print(*nameList, sep = ",");

# Print items in a list separated by a hyphen with spaces on either side
print(*nameList, sep = " - ");

# Print items in list separated on different lines with new line character
print(*nameList, sep = "\n");

This gives us the following output:

James Chad Hulk Hogan He-Man
James,Chad,Hulk Hogan,He-Man
James - Chad - Hulk Hogan - He-Man
James
Chad
Hulk Hogan
He-Man

Printing a List in Python Using the For Loop

Another way to print all of the values from a list is to use a for loop. Here is how you use a for loop to print the values from a list in Python:

# Creating a list in Python
nameList = ["James","Chad", "Hulk Hogan", "He-Man"];

# Print the items in a list using a For Loop
for x in range(len(nameList)):
    print (nameList[x])

As you might suspect, this will result in the following output:

James
Chad
Hulk Hogan
He-Man

Other Notes About Python Lists

An interesting thing to consider about lists is the fact that they can contain multiple copies of the same items or objects that are not unique. For instance, the following is a perfectly acceptable list:

nameListTwo = ["James","James", "James", "Of the Jungle"];

Further, lists do not need to contain more than one object. You could easily create a list such as:

singleNameList = ["James"];

You can also have two lists that contain the same exact information but that are not considered the same when compared, so long as the items in those lists are ordered differently. Consider the following code example, where we create two lists with the same values and then compare them to one another:

# Create two lists that contain the same data elements, but in a different order
listA = ["A","B","C","D"];
listB = ["A","C","B","D"];

# If statements comparing two lists
if listA == listB:
	print("listA and listB are equal!")
elif listA != listB:
	print("listA and listB are not equal!")

Can you guess the result of this program? If you run this Python code, it will create the output below:

listA and listB are not equal!

Now let’s recreate the lists and have the same exact values in the same exact order and see what happens when we compare them – here is the code:

# Create two lists that contain the same data elements in the same order
listA = ["A","B","C","D"];
listB = ["A","B","C","D"];

# If statements comparing two lists
if listA == listB:
	print("listA and listB are equal!")
elif listA != listB:
	print("listA and listB are not equal!")

Now when we run the program, we see that the lists are considered to be the same:

listA and listB are equal!

Code Samples for Working with Lists in Python

Below, you can find every example in this article. Feel free to modify and experiment with sections of the code to try and get different results.

Taken from the file, PythonListExamples.py:

# Creating some lists in Python

nameList = ["James","Chad", "Hulk Hogan", "He-Man"];
ageList = [42, 89, 17, 4];
mixedList = ["Pumpkin", 2000, "Steve Austin", 2999];

# Printing the values in lists

# Printing the first value in the list
print("The first item in nameList is: ")
print(nameList[0]);

# Printing the second value in the list

print("The second item in nameList is: ")
print(nameList[1]);

# Printing items in a range or multiple values in a list

print("The second through fourth values in the list are: ")
print(nameList[0:3]);

# Printing all of the items in a list with the print function

print("Here are all of the items in nameList: ")
print(nameList);

# Printing all items from a list with *

print(*nameList);

# Print items in list separated by a comma
print(*nameList, sep = ",");

# Print items in a list separated by a hyphen with spaces on either side
print(*nameList, sep = " - ");

# Print items in list separated on different lines with new line character
print(*nameList, sep = "\n");


# Create two lists that contain the same data elements, but in a different order
listA = ["A","B","C","D"];
listB = ["A","C","B","D"];

# If statements comparing two lists

if listA == listB:
	print("listA and listB are equal!")
elif listA != listB:
	print("listA and listB are not equal!")

# Create two lists that contain the same data elements in the same order
listA = ["A","B","C","D"];
listB = ["A","B","C","D"];

# If statements comparing two lists

if listA == listB:
	print("listA and listB are equal!")
elif listA != listB:
	print("listA and listB are not equal!")

Latest Posts

Related Stories