Defining Lists with Square Brackets

๐Ÿท๏ธ Lists and List Operations / Creating and Accessing Lists

In Python, a list is one of the most versatile and commonly used data structures. Think of it as a container that can hold multiple items in a specific order. Whether you're storing server names, IP addresses, configuration values, or log entries, lists give you a simple way to group related data together. The most straightforward way to create a list is by using square brackets [].


โš™๏ธ What is a List?

  • A list is an ordered collection of items, meaning each item has a position (index) starting from 0.
  • Lists can hold different data types in the same list โ€” strings, numbers, booleans, or even other lists.
  • Lists are mutable, which means you can change, add, or remove items after the list is created.
  • Items inside a list are separated by commas.

๐Ÿ“Š Creating a List with Square Brackets

  • To define a list, simply place your items inside square brackets [].
  • The syntax is: variable_name = [item1, item2, item3]

Example โ€” An empty list:
empty_list = []

Example โ€” A list of server names (strings):
servers = ["web01", "db01", "cache01"]

Example โ€” A list of numbers (integers):
ports = [80, 443, 22, 3306]

Example โ€” A mixed data type list:
mixed = ["nginx", 1.5, True, 443]


๐Ÿ› ๏ธ Key Characteristics of Lists

  • Ordered: Items maintain the order in which you define them.
  • Indexed: Each item has a position. The first item is at index 0, second at 1, and so on.
  • Flexible: You can store any data type โ€” strings, integers, floats, booleans, even other lists.
  • Dynamic: Lists can grow or shrink as needed. You are not limited to a fixed size.

๐Ÿ•ต๏ธ Accessing Items in a List

  • Use the list name followed by the index number inside square brackets to access a specific item.
  • Indexing starts at 0.

Example โ€” Accessing the first server:
servers = ["web01", "db01", "cache01"]
first_server = servers[0] โ†’ This gives you "web01"

Example โ€” Accessing the last item using negative indexing:
last_server = servers[-1] โ†’ This gives you "cache01"

Example โ€” Accessing the second item:
second_server = servers[1] โ†’ This gives you "db01"


๐Ÿ“‹ Comparison: List vs Other Simple Data Types

Feature List String Integer
Holds multiple items โœ… Yes โŒ No (single text) โŒ No (single number)
Mutable (changeable) โœ… Yes โŒ No (immutable) โŒ No (immutable)
Ordered โœ… Yes โœ… Yes N/A
Indexed access โœ… Yes โœ… Yes N/A
Mixed data types โœ… Yes โŒ No โŒ No

๐Ÿงช Practical Examples for Engineers

Example 1 โ€” Storing environment names:
environments = ["dev", "staging", "prod"]
current_env = environments[1] โ†’ Result: "staging"

Example 2 โ€” Storing IP addresses as strings:
ip_list = ["10.0.1.1", "10.0.1.2", "10.0.1.3"]
first_ip = ip_list[0] โ†’ Result: "10.0.1.1"

Example 3 โ€” Storing configuration flags:
config_flags = [True, False, True, False]
first_flag = config_flags[0] โ†’ Result: True

Example 4 โ€” A list of lists (nested list):
network_config = [["web", "10.0.1.1"], ["db", "10.0.1.2"]]
first_pair = network_config[0] โ†’ Result: ["web", "10.0.1.1"]
first_service = network_config[0][0] โ†’ Result: "web"


โœ… Summary

  • Lists are defined using square brackets [].
  • Items are separated by commas.
  • Lists are ordered, mutable, and can hold mixed data types.
  • Access items using index numbers starting from 0.
  • Negative indexing (e.g., -1) lets you access items from the end of the list.

Lists are a foundational building block in Python. Once you master defining and accessing them with square brackets, you'll be ready to explore more powerful list operations like adding, removing, and iterating over items.


A list in Python is an ordered collection of items created by placing values inside square brackets [], separated by commas.


๐ŸŸข Example 1: Creating an empty list

This example shows how to define a list with no items inside it.

empty_list = []

๐Ÿ“ค Output: []


๐ŸŸข Example 2: Creating a list of numbers

This example shows a list containing three integer values.

numbers = [10, 20, 30]

๐Ÿ“ค Output: [10, 20, 30]


๐ŸŸข Example 3: Creating a list of strings

This example shows a list containing text values.

cities = ["New York", "London", "Tokyo"]

๐Ÿ“ค Output: ['New York', 'London', 'Tokyo']


๐ŸŸข Example 4: Creating a mixed-type list

This example shows a list containing different data types together.

mixed = [42, "engineer", 3.14, True]

๐Ÿ“ค Output: [42, 'engineer', 3.14, True]


๐ŸŸข Example 5: Creating a list from variables

This example shows how to use existing variables to build a list.

sensor_id = "TEMP-001"
reading = 23.5
unit = "Celsius"

log_entry = [sensor_id, reading, unit]

๐Ÿ“ค Output: ['TEMP-001', 23.5, 'Celsius']


๐Ÿ“Š Quick Reference: Defining Lists

Syntax Description Example
[] Empty list data = []
[value1, value2, ...] List with items ports = [80, 443, 22]
[var1, var2, ...] List from variables config = [host, port, timeout]

In Python, a list is one of the most versatile and commonly used data structures. Think of it as a container that can hold multiple items in a specific order. Whether you're storing server names, IP addresses, configuration values, or log entries, lists give you a simple way to group related data together. The most straightforward way to create a list is by using square brackets [].


โš™๏ธ What is a List?

  • A list is an ordered collection of items, meaning each item has a position (index) starting from 0.
  • Lists can hold different data types in the same list โ€” strings, numbers, booleans, or even other lists.
  • Lists are mutable, which means you can change, add, or remove items after the list is created.
  • Items inside a list are separated by commas.

๐Ÿ“Š Creating a List with Square Brackets

  • To define a list, simply place your items inside square brackets [].
  • The syntax is: variable_name = [item1, item2, item3]

Example โ€” An empty list:
empty_list = []

Example โ€” A list of server names (strings):
servers = ["web01", "db01", "cache01"]

Example โ€” A list of numbers (integers):
ports = [80, 443, 22, 3306]

Example โ€” A mixed data type list:
mixed = ["nginx", 1.5, True, 443]


๐Ÿ› ๏ธ Key Characteristics of Lists

  • Ordered: Items maintain the order in which you define them.
  • Indexed: Each item has a position. The first item is at index 0, second at 1, and so on.
  • Flexible: You can store any data type โ€” strings, integers, floats, booleans, even other lists.
  • Dynamic: Lists can grow or shrink as needed. You are not limited to a fixed size.

๐Ÿ•ต๏ธ Accessing Items in a List

  • Use the list name followed by the index number inside square brackets to access a specific item.
  • Indexing starts at 0.

Example โ€” Accessing the first server:
servers = ["web01", "db01", "cache01"]
first_server = servers[0] โ†’ This gives you "web01"

Example โ€” Accessing the last item using negative indexing:
last_server = servers[-1] โ†’ This gives you "cache01"

Example โ€” Accessing the second item:
second_server = servers[1] โ†’ This gives you "db01"


๐Ÿ“‹ Comparison: List vs Other Simple Data Types

Feature List String Integer
Holds multiple items โœ… Yes โŒ No (single text) โŒ No (single number)
Mutable (changeable) โœ… Yes โŒ No (immutable) โŒ No (immutable)
Ordered โœ… Yes โœ… Yes N/A
Indexed access โœ… Yes โœ… Yes N/A
Mixed data types โœ… Yes โŒ No โŒ No

๐Ÿงช Practical Examples for Engineers

Example 1 โ€” Storing environment names:
environments = ["dev", "staging", "prod"]
current_env = environments[1] โ†’ Result: "staging"

Example 2 โ€” Storing IP addresses as strings:
ip_list = ["10.0.1.1", "10.0.1.2", "10.0.1.3"]
first_ip = ip_list[0] โ†’ Result: "10.0.1.1"

Example 3 โ€” Storing configuration flags:
config_flags = [True, False, True, False]
first_flag = config_flags[0] โ†’ Result: True

Example 4 โ€” A list of lists (nested list):
network_config = [["web", "10.0.1.1"], ["db", "10.0.1.2"]]
first_pair = network_config[0] โ†’ Result: ["web", "10.0.1.1"]
first_service = network_config[0][0] โ†’ Result: "web"


โœ… Summary

  • Lists are defined using square brackets [].
  • Items are separated by commas.
  • Lists are ordered, mutable, and can hold mixed data types.
  • Access items using index numbers starting from 0.
  • Negative indexing (e.g., -1) lets you access items from the end of the list.

Lists are a foundational building block in Python. Once you master defining and accessing them with square brackets, you'll be ready to explore more powerful list operations like adding, removing, and iterating over items.

Interactive Views

You are currently in ๐Ÿ“š All-in-One mode. Use the tabs at the top to switch to ๐Ÿ“– Theory Only or ๐Ÿ’ป Code Only views.

A list in Python is an ordered collection of items created by placing values inside square brackets [], separated by commas.


๐ŸŸข Example 1: Creating an empty list

This example shows how to define a list with no items inside it.

empty_list = []

๐Ÿ“ค Output: []


๐ŸŸข Example 2: Creating a list of numbers

This example shows a list containing three integer values.

numbers = [10, 20, 30]

๐Ÿ“ค Output: [10, 20, 30]


๐ŸŸข Example 3: Creating a list of strings

This example shows a list containing text values.

cities = ["New York", "London", "Tokyo"]

๐Ÿ“ค Output: ['New York', 'London', 'Tokyo']


๐ŸŸข Example 4: Creating a mixed-type list

This example shows a list containing different data types together.

mixed = [42, "engineer", 3.14, True]

๐Ÿ“ค Output: [42, 'engineer', 3.14, True]


๐ŸŸข Example 5: Creating a list from variables

This example shows how to use existing variables to build a list.

sensor_id = "TEMP-001"
reading = 23.5
unit = "Celsius"

log_entry = [sensor_id, reading, unit]

๐Ÿ“ค Output: ['TEMP-001', 23.5, 'Celsius']


๐Ÿ“Š Quick Reference: Defining Lists

Syntax Description Example
[] Empty list data = []
[value1, value2, ...] List with items ports = [80, 443, 22]
[var1, var2, ...] List from variables config = [host, port, timeout]