Skip to content

How to Create a List in Python: Examples, Empty Lists and Common Mistakes

Create a Python list with square brackets, such as numbers = [1, 2, 3], or with list() when converting an iterable. Lists preserve order, support duplicates, and can hold values of different types.

What is a Python list?

A Python list is an ordered, mutable collection. Unlike a string, which is immutable, a list can hold numbers, strings, dictionaries, and other lists in the same sequence. Indexes start at zero.

Lists are a useful tool for preserving a sequence of data and further iterating over it.

The important characteristics of Python lists are:

  • Lists preserve insertion order and allow access by zero-based index, including negative indexes.
  • Lists allow duplicate values and can contain objects of different types.
  • Lists can be nested; for example, [[1, 2], [3, 4]] contains two inner lists.
  • Lists are mutable: you can replace, append, or remove elements. Tuples are immutable.
  • Lists can grow or shrink as elements are added or removed.

How to Make a List in Python

Create a list by placing comma-separated items inside square brackets, for example [1, 2, 3]. Items may have different types, and [] creates an empty list. Use list(iterable) to convert an iterable such as a tuple or string.

The following examples demonstrate lists of integers, floats, strings, mixed values, and an empty list.

Creating an Integer List

list_integer = [1,2,3]
print("List of Integer:",list_integer)

Output:

# Output:
List of Integer: [1, 2, 3]

Creating a list of floats

list_float = [2.3,5.6,9.8]
print("List of Float:",list_float)

Output:

# Output:
List of Float: [2.3, 5.6, 9.8]

Creating a String List

list_string = ["Learn Python", 'Pickupbrain']
print("List of String:",list_string)

Output:

# Output:
List of String: ['Learn Python', 'Pickupbrain']

Creating a Mixed List

A Python list can mix data types without declaring a single element type.

list_mixed = [1,2,3,'Hello',6.7]
print("List of Mixed values:",list_mixed)

Output:

# Output:
List of Mixed values: [1, 2, 3, 'Hello', 6.7]

Creating an Empty List

Create an empty list when you plan to collect values later, for example with append().

list_empty = []
print("Empty List:",list_empty)

Output:

# Empty List: []

Adding an element in a list

To add an element in a list, there are three methods:

  • Append() Method:
    It adds an element to the end of the list. It does not return the new list but instead modifies the original.
    list.append(element)
  • Extend() Method:
    It iterates over its argument by adding each element to the list and extending the list.
    list1.extend(list2)
  • Insert() Method:
    It inserts an element to the list at a given index. Unlike list.index(value), which searches for an existing value, list.insert(index, value) adds a new element at a specified position.
    list.insert(index, element)

Creating and modifying lists: key takeaways

Use [] for an empty list, square brackets for list literals, and list(iterable) to convert an iterable. Lists preserve order and duplicates; methods such as append() and extend() modify the existing list and return None.

Indexing, slicing and nested lists

An empty list is [] or list(). Use fruits = [‘apple’, ‘pear’] for strings, mixed = [1, ‘two’, True] for mixed types, and matrix = [[1, 2], [3, 4]] for a nested list. Access fruits[0] to get ‘apple’, fruits[-1] to get the last item, and fruits[0:1] for a one-item slice. Add a single element with append(), or multiple elements from an iterable with extend().

Common mistakes and troubleshooting

Avoid using list as a variable name because it shadows Python’s built-in list constructor. A list is mutable: assignment alias = fruits refers to the same object, while fruits.copy() makes a shallow copy. For independent nested lists, use copy.deepcopy() when necessary.

Frequently asked questions

Is [] the same as list()? Both create a new empty list. What does list(‘abc’) return? [‘a’, ‘b’, ‘c’]. Does append([1, 2]) add two items? No, it adds one nested list; extend([1, 2]) adds two items. What happens with an invalid index? Reading values[99] raises IndexError; check len(values) or handle the exception. See Python’s official data-structures tutorial.

Leave a Reply

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