How to Create a List in Python?

Estimated read time 1 min read

To create a list in Python, you can use square brackets [] or the list() function. Here are some examples:

  1. Using square brackets:
my_list = [1, 2, 3, "apple", "banana", "cherry"]
  1. Using the list() function:
my_list = list(("John", "Doe", 35))

In the first example, we create a list with six elements that include integers and strings. The elements are enclosed in square brackets and separated by commas.

In the second example, we use the list() function to create a list from a tuple that contains three elements (a string and two integers). Note that when using the list() function, you need to enclose the tuple in parentheses.

You can add, remove, or modify elements in a list using various methods and operators in Python.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply