In Python, an array is an object that provides a mechanism for storing several data items with only one identifier, thereby simplifying the task of data management. The array is beneficial if you need to store a group of elements of the same data type.
Python arrays can increase or decrease their size dynamically.
A list is a type of array in Python. Python arrays have 4 types as mentioned below:
- List is a collection that is ordered and changeable. Allows duplicate members.
- Tuple is a collection that is ordered and unchangeable. Allows duplicate members.
- Set is a collection that is unordered and unindexed. No duplicate members.
- Dictionary is a collection that is unordered, changeable, and indexed. No duplicate members.
You can use any type of Python array according to the nature of your program.
Python List
A list is a collection that is ordered and changeable. In Python, we will write a list with square brackets.
Code
list_a = ["red", "yellow", "orange"]
print(list_a)
PythonOutput: [‘red’, ‘yellow’, ‘orange’] | |
Access Item
Access the third item from the list.
Code
list_a = ["red", "yellow", "orange"]
print(list_a[2]) # Accessing the element at index 2 (which is the third element in the list)
PythonOutput: orange | |
Change Item from List
You can also change a specific item from a list using its index.
Code
list_a = ["red", "yellow", "orange"]
list_a[2] = "Green"
print(list_a)
PythonOutput: [‘red’, ‘yellow’, ‘Green’] | |
Print a List using for loop
We can also use for loop to print list items in one go except printing one by one.
Code
list_a = ["red", "yellow", "orange"]
for x in list_a:
print(x)
PythonOutput: red yellow orange | |
Check if the Item exists or not
Code
list_a = ["red", "yellow", "orange"]
if "red" in list_a:
print("Yes, 'red' is in the list")
PythonOutput: Yes, ‘red’ is in the list | |
Python List Methods
Here is a list of some commonly used Python list methods.
Method | Description |
---|---|
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
Add a new item to the List
Add Items in the list using the Python list method called append ():
Code
list_a = ["red", "yellow", "orange"]
list_a.append("Green")
print(list_a)
PythonOutput: [‘red’, ‘yellow’, ‘orange’, ‘Green’] | |
Pop an item from the List
Pop Items in the list using the pop () method:
Code
print(list_a.pop()) # This will remove and return the last element from list_a
print(list_a)
PythonOutput: [“red”, “orange”] | |
Remove item from the List
We can remove our list of Items using the remove () method:
Code
list_a = ["red", "Yellow", "orange"]
list_a.remove("Yellow")
print(list_a)
PythonOutput: [‘red’, ‘orange’] | |
Sort List Items
We can order our list Items using the sort () method:
Code
list_b = [2, 4, 7, 8, 9, 3, 0]
list_b.sort()
print(list_b)
PythonOutput: [0, 2, 3, 4, 7, 8, 9] | |
Reverse List Items
We can reverse our list of Items using the reverse () method:
Code
list_b = [2, 4, 7, 8, 9, 3, 0]
list_b.reverse()
print(list_b)
PythonOutput: [0, 3, 9, 8, 7, 4, 2] | |
You can also read: