Creating Python Lists (list): Basic Usage of [] and list()

The list in Python is a very powerful and flexible data type that allows you to store multiple pieces of data in a specific order. Each piece of data (element) stored in a list can be changed, added, or deleted later (this property is called mutable).

Lists are used to handle collections of data, such as a group of numbers, user names, or a mix of different data types (numbers, strings, booleans).

This article explains the two basic ways to create lists in Python: using [] (square brackets) and the list() constructor.


目次

1. Creating Lists with [] (Square Brackets)

The most common and intuitive way to create a list is to enclose elements in [] (square brackets) and separate each element with a comma (,).

Lists with the Same Data Type

# List of numbers (int)
scores = [80, 95, 72, 88]

# List of strings (str)
user_names = ["sato", "suzuki", "tanaka"]

print(scores)
print(user_names)

Output:

[80, 95, 72, 88]
['sato', 'suzuki', 'tanaka']

Lists with Mixed Data Types

Python lists can store different data types, such as numbers, strings, and booleans, at the same time.

# Mixing string, number, and boolean
user_data = ["Yamada", 45, False]
print(user_data)

Output:

['Yamada', 45, False]

Creating an Empty List

You can create an empty list that holds no elements. This is often used when you plan to add data to the list later in the program.

# Empty list
item_list = []
print(item_list)

Output:

[]

2. Creating Lists with the list() Constructor

The other method is to use the built-in list() constructor (which acts like a function).

Creating an Empty List

Just like with [], calling list() without any arguments creates an empty list.

# Create an empty list with list()
empty_list_via_constructor = list()
print(empty_list_via_constructor)

Output:

[]

Creating from Iterables

The main use of the list() constructor is to convert other “Iterables” (objects where you can extract elements one by one, like strings or range) into a list.

Creating a List from a String When you pass a string to list(), it creates a list broken down into individual characters.

# Convert the string "Python" to a list
char_list = list("Python")
print(char_list)

Output:

['P', 'y', 't', 'h', 'o', 'n']

Creating a List from range() Combining list() with range() (which generates a sequence of numbers) allows you to easily create a list of consecutive numbers.

# Create a list of numbers from 101 to 103 (stops before 104)
id_list = list(range(101, 104))
print(id_list)

Output:

[101, 102, 103]

Summary

We discussed two ways to create lists in Python.

  • [] (Square Brackets): The most common method. Used to create lists by directly specifying elements, like [80, 95] or [].
  • list() Constructor: While it can create empty lists, it is mainly used to convert other iterable objects into lists, such as list("Python") or list(range(10)).
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次