Understanding and Using Lists in Python

A list is an ordered collection of items, which can be of any type. It’s one of the most powerful data types in Python. This article will help you understand how lists are used and how to print them e …

Updated November 30, 2023

A list is an ordered collection of items, which can be of any type. It’s one of the most powerful data types in Python. This article will help you understand how lists are used and how to print them effectively.

Lists in Python A list in python is a special kind of iterable which means it has an order and can be indexed, sliced or looped through. They are mutable i.e., they can be changed after creation.

Lists are created using square brackets [] and items are separated by commas. Here’s how you could create a list in Python:

fruits = ["apple", "banana", "cherry"]
print(fruits)

Printing Lists The print statement can be used to display the entire list at once or you can iterate through each item in the list.

  1. Print all items:
fruits = ["apple", "banana", "cherry"]
print(fruits)
  1. Print each item one by one:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

In both cases, the output would be:

['apple', 'banana', 'cherry']
apple
banana
cherry

The square brackets are only necessary if you want to display all items at once. If you use print without brackets, it will print each item on a new line as per its order in the list.

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!