Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Declare a List in Python

Learn how to create and work with lists in Python, a fundamental data structure in programming. …


Updated June 4, 2023

Learn how to create and work with lists in Python, a fundamental data structure in programming.

As a beginner or experienced programmer, understanding how to declare a list in Python is essential for working with data. In this tutorial, we’ll explore the concept of lists, their characteristics, and step-by-step instructions on how to create them.

Definition of a List

In Python, a list is a collection of items that can be of any data type, including strings, integers, floats, and other lists. Lists are ordered, meaning they maintain the order in which elements were added. They are denoted by square brackets [].

Characteristics of a List

Before diving into how to declare a list, it’s essential to understand its characteristics:

  • Ordered: Lists maintain the order in which elements were added.
  • Indexed: Each element in a list has an index or key that allows for easy access and modification.
  • Mutable: Lists can be modified after creation.

Declaring a List

Declaring a list in Python is straightforward. You can create a list by placing items within square brackets []. Here’s a simple example:

Code Snippet

# Declare an empty list
my_list = []

# Declare a list with initial elements
fruits = ['Apple', 'Banana', 'Cherry']
  • In the first example, we declare an empty list my_list by assigning an empty pair of square brackets to it.
  • In the second example, we create a list fruits and assign it a list of strings directly.

Adding Elements to a List

You can add elements to a list using various methods:

Code Snippet

# Append a new element to the end of the list
my_list.append('Orange')

# Insert an element at a specific position in the list
fruits.insert(1, 'Grapes')

# Add multiple elements to the list
new_fruits = ['Mango', 'Pineapple']
fruits.extend(new_fruits)
  • The append() method adds a new element to the end of the list.
  • The insert() method allows you to add an element at a specific position in the list.
  • The extend() method is used to add multiple elements from another list or iterable.

Accessing and Modifying List Elements

You can access elements in a list using their index. Here’s how:

Code Snippet

# Access the first element of the list (index 0)
print(my_list[0])

# Modify an existing element in the list
my_list[0] = 'Watermelon'

# Remove the last element from the list
my_list.pop()
  • To access a specific element, use its index within square brackets [].
  • Use the assignment operator to modify an existing element.
  • The pop() method removes and returns the last element in the list.

Conclusion

In this tutorial, we’ve learned how to declare lists in Python. We explored their characteristics, how to create them with initial elements or empty, and various methods for adding new elements. Finally, we discussed accessing and modifying existing elements using their index. With these skills, you’re now equipped to work effectively with lists in your Python programming endeavors.

References

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp