Python Lists are used to store multiple items in a single variable. We create a list using an angle bracket “[]” and separated it by commas.
The list is mutable, which means it may be altered or updated after it is created.
Example of a List in Python:
Here is an example of a Python List using the angle bracket “[]”.
1 2 | days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] print(days) |
Output:
1 | ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] |
Accessing Python List Elements
Getting the value of an element in a list is as simple as using the index in the list. Indexes are assigned from 0 in Python. We can also access items with negative indexes in Python. Negative indexes indicate entries counted from the list’s end.
1 2 | days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] print(days[0]) |
Output:
1 | Sunday |
Concatenation and Replication of Lists in Python
List concatenation is the process of combining the contents of two lists into one.
1 2 3 4 5 | name1 = ["John", "Rahul", "David"] name2 = ["Sohel", "Karan"] names = name1 + name2 print(names) |
Output:
1 | ['John', 'Rahul', 'David', 'Sohel', 'Karan'] |
List replication is the process of copying the contents of a list a finite number of times into the same or another list.
1 2 3 | name1 = ["John", "Rahul", "David"] name1 = name1 * 2 print(name1) |
Output:
1 | ['John', 'Rahul', 'David', 'John', 'Rahul', 'David'] |
Adding Items to Lists
We can use the insert function to add an item to a specific index of a list. It takes two arguments the first argument will be the index and the second argument will be the item.
1 2 3 | names = ["John", "Rahul", "David"] names.insert(1, "Rakesh") print(names) |
Output:
1 | ['John', 'Rakesh', 'Rahul', 'David'] |
Using the append function, we can append an element to the end of a list.
1 2 3 | names = ["John", "Rahul", "David"] names.append("Rakesh") print(names) |
Output:
1 | ['John', 'Rahul', 'David', 'Rakesh'] |
Delete or Remove items from Lists
We can remove a specific item from a list using the del keyword.
1 2 3 4 | name1 = ["John", "Rahul", "David"] print(name1) del name1[1] print(name1) |
Output: Rahul will be removed from the name1 list.
1 2 | ['John', 'Rahul', 'David'] ['John', 'David'] |
You can also remove the list item by passing the item name as an argument into the remove keyword.
1 2 3 | numbers = [1, 2, 4] numbers.remove(2) print(numbers) #[1, 4] |
Looping/Iterating through Lists
The following example shows how to iterate over all of the items in a list.
1 2 3 | days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] for day in days: print(day) |
Output:
1 2 3 4 5 6 7 8 | Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday |
Python List Sorting
We use the
sort()
method to arrange the items on the list in a specific order. It sorts the items in ascending order by default.
1 2 3 4 5 6 | names = ["John", "Rahul", "David"] names.sort() print(names) numbers = [3,5,1,4,2] numbers.sort() print(numbers) |
Output:
1 2 | ['David', 'John', 'Rahul'] [1, 2, 3, 4, 5] |
We can sort in descending order using reverse argument in the sort function.
1 2 3 4 5 6 | names = ["John", "Rahul", "David"] names.sort(reverse = True) print(names) numbers = [3,5,1,4,2] numbers.sort(reverse = True) print(numbers) |
Output:
1 2 | ['Rahul', 'John', 'David'] [5, 4, 3, 2, 1] |
Reverse List in Python
reverse()
: This reverses the order of list elements in python.
1 2 3 | numbers = [3,5,1,4,2] names.reverse() print(numbers) |
Output:
1 | [2, 4, 1, 5, 3] |
Stack
The two operations in Python allow for the intuitive usage of lists as a stack.
- append()
- pop().
1 2 3 4 | stack = [3] stack.append(42) # [3, 42] stack.pop() # 42 (stack: [3]) stack.pop() # 3 (stack: []) |