In this tutorial, we will learn how to create a list in Python. A list is a data structure that can store multiple items in a single variable. It is an ordered collection of elements, which can be of different types, such as integers, strings, or even other lists. It is one of the most commonly used data structures in Python and is very useful in various programming scenarios.
Step 1: Creating an Empty List
There are two ways to create an empty list in Python:
- Using square brackets
- Using the
list()
constructor
Here is an example of each:
1 2 |
empty_list1 = [] empty_list2 = list() |
Both empty_list1
and empty_list2
will be empty lists.
Step 2: Creating a List with Elements
To create a list with elements, you can either use square brackets and separate the elements with commas or use the list()
constructor with an iterable (e.g. a string or a tuple) as the argument.
Here is an example of each:
1 2 3 4 5 6 |
number_list = [1, 2, 3, 4, 5] name_list = ["Alice", "Bob", "Carol"] mixed_list = [1, "two", 3.0, [4, 5]] number_list2 = list((1, 2, 3, 4, 5)) string_list = list("Python") |
Step 3: Adding Elements to a List
To add elements to a list, you can use the following methods:
append()
– Adds an element at the end of the listinsert()
– Adds an element at the specified positionextend()
– Adds the elements of an iterable (e.g. another list, a tuple, or a string) to the end of the list
Here is an example of each:
1 2 3 4 5 6 7 8 9 10 |
fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) fruits.insert(1, "grape") print(fruits) fruits.extend(["kiwi", "melon"]) print(fruits) |
Output:
['apple', 'banana', 'cherry', 'orange'] ['apple', 'grape', 'banana', 'cherry', 'orange'] ['apple', 'grape', 'banana', 'cherry', 'orange', 'kiwi', 'melon']
Step 4: Removing Elements from a List
To remove elements from a list, you can use the following methods:
remove()
– Removes the first occurrence of a specified elementpop()
– Removes the element at the specified index, or the last element if no index is specifiedclear()
– Removes all elements from the list
Here is an example of each:
1 2 3 4 5 6 7 8 9 10 |
fruits = ["apple", "banana", "cherry", "orange"] fruits.remove("banana") print(fruits) fruits.pop(1) print(fruits) fruits.clear() print(fruits) |
Output:
['apple', 'cherry', 'orange'] ['apple', 'orange'] []
Step 5: Accessing Elements in a List
You can access elements in a list by their index, with the first element having an index of 0. You can also use negative indexing to access elements from the end of the list, with -1 being the last element’s index.
Here is an example:
1 2 3 4 5 6 7 |
fruits = ["apple", "banana", "cherry", "orange"] print(fruits[0]) print(fruits[-1]) fruits[1] = "grape" print(fruits) |
Output:
apple orange ['apple', 'grape', 'cherry', 'orange']
Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
empty_list1 = [] empty_list2 = list() number_list = [1, 2, 3, 4, 5] name_list = ["Alice", "Bob", "Carol"] mixed_list = [1, "two", 3.0, [4, 5]] number_list2 = list((1, 2, 3, 4, 5)) string_list = list("Python") fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) fruits.insert(1, "grape") print(fruits) fruits.extend(["kiwi", "melon"]) print(fruits) fruits.remove("banana") print(fruits) fruits.pop(1) print(fruits) fruits.clear() print(fruits) fruits = ["apple", "banana", "cherry", "orange"] print(fruits[0]) print(fruits[-1]) fruits[1] = "grape" print(fruits) |
Conclusion
In this tutorial, we learned how to create a list in Python, add and remove elements, and access elements in the list. Lists are an essential data structure that can be used in various programming scenarios and are an important part of the Python language.