Python List Literals: Complete Guide with Syntax and Examples

Understanding Python List Literals

Many Python programs need to store multiple values together, such as names, numbers, prices, and scores. These values can be written directly in Python code using a list literal or created during program execution.

In this guide, you’ll learn what Python list literals are, their syntax, how they work, and how to use them with simple examples.

🚀 Getting Started: Before diving into list literals, it’s worth reading our Learn about Python literals to understand where they fit in Python.

💡 Tip: To understand how Python stores and works with lists internally, you can explore our guide on Python list() function.

Introduction: What is a List Literal?

Definition: A Python list literal is a collection of one or more items written directly in Python code and enclosed within square brackets ([]). Individual items are separated by commas, and each item can be any valid Python object, including numbers, strings, Boolean values, or even other lists.

Example: In the statement numbers = [1, 2, 3], the value [1, 2, 3] is a list literal because it is written directly in the code and enclosed within square brackets.

Before exploring the different forms of Python list literals, let’s first understand their syntax.

How to Write Python List Literals

Python allows list literals to be written directly in the source code without using any special function or constructor. A list literal is created by enclosing one or more items within square brackets and separating them with commas.

The syntax below illustrates the general structure of a Python list literal.

Syntax

list_name = [item1, item2, item3, ...]

Explanation: Here, list_name is the variable used to store the list, while item1, item2, and item3 represent the values stored in the list. Each item is separated by a comma, and the entire collection is enclosed within square brackets.

Component Description
list_name The name of the variable used to store the list.
= The assignment operator used to assign the list literal to a variable.
[ ] Square brackets that define the list literal.
item1, item2, ... The values stored in the list. Each item is separated by a comma and may be of the same or different data type.

Let’s explore the different forms of Python list literals with simple examples.

Forms of Python List Literals

Python list literals can be written in different forms depending on the type of data they contain:

  1. List of Integers
  2. List of Float Values
  3. Mixed Data Type List
  4. Nested List


Let’s explore each form of Python list literal with explanations and examples.

1. List of Integers

List literals can store integer values.

Example: Integer List Literal
# List of integers

numbers = [1, 2, 3, 4, 5]

print(numbers)


# Output
[1, 2, 3, 4, 5]

Explanation: The list contains only integer values. Each value is stored in the order in which it appears.

⬆ Move to Top

2. List of Float Values

List literals can also store floating-point numbers.

Example: Float List Literal
# List of float values

prices = [19.99, 45.50, 12.75, 3.90]

print(prices)


# Output
[19.99, 45.5, 12.75, 3.9]

Explanation: Every element in the list is a floating-point value representing a decimal number.

⬆ Move to Top

3. Mixed Data Type List

A list can store different types of values together.

Example: Mixed Data Type List
# Mixed data type list

mixed_list = [5, "Hello", 3.14, True]

print(mixed_list)


# Output
[5, 'Hello', 3.14, True]

Explanation: This list contains four different data types: an integer, a string, a float, and a Boolean value. Python lists can store different types of data in the same collection.

⬆ Move to Top

4. Nested List

A list can also contain one or more lists as its elements.

Example: Nested List
# Nested list

nested_list = [1, 2, [3, 4, 5], 6]

print(nested_list)


# Output
[1, 2, [3, 4, 5], 6]

Explanation: The third element is another list. Nested lists are commonly used to represent tables, matrices, grids, and other hierarchical data structures.

⬆ Move to Top

Key Examples at a Glance: List Literals

The following table summarizes the different forms of Python list literals:

Type Format Example Meaning
Integer List [a, b, c] [1, 2, 3, 4, 5] Stores whole numbers
Float List [a, b, c] [19.99, 45.50, 12.75, 3.90] Stores decimal values
Mixed Data List [a, "text", 1.5, True] [5, "Hello", 3.14, True] Stores multiple data types
Nested List [a, [b, c], d] [1, 2, [3, 4, 5], 6] Contains another list as an element

Key Takeaways: List Literals

Here are the key points to remember about Python list literals:

  • A Python list literal is created using square brackets ([]).
  • List items are separated by commas.
  • Lists preserve the order of their elements.
  • Lists are mutable, allowing items to be added, removed, or modified after creation.
  • A list can store values of the same or different data types.
  • Lists can contain other lists, making it easy to represent nested or hierarchical data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top