Python arrays are a special variable that have the ability to hold more than one value. For instance, if were a list of of car models, those models could be stored into single variables. The array can hold a fix number of elements and these elements must be the same type (string, integer). When creating an array, each item that is stored is referred to as an element and the location of each element is referred to as an index.
Python Array Methods
Python Array Creation
Creating an array is similar to creating a list in Python, which can be easily done by using square brackets and surrounding the values with quotes if needed (strings).
Input:
1 2 3 | tech_companies = ["Facebook", "Google", "Amazon", "Microsoft", "IBM"] print(tech_companies) |
Output:
1 | ['Facebook', 'Google', 'Amazon', 'Microsoft', 'IBM'] |
Python Array Access
In order to access the elements in the array, the index number will be executed to refer to the array element.
Array Example 1
Input:
1 | print(f"What's the best tech company to work for? {tech_companies[1]}") |
Output:
1 | What's the best tech company to work for? Google |
Array Example 2
The below is accessing the list by using random and randomly picking an element from the list to call in the print statement.
Input:
1 2 3 4 5 6 | import random for name in random.sample(tech_companies, 1): best_tech = name print(f"What's the best tech company to work for? {best_tech}") |
Output:
1 | What's the best tech company to work for? Amazon |
Python Change List Item
This is similar to the list access, but we will create a variable with the list item referencing the index number and provide the update.
Input:
1 2 3 4 5 6 | print(f"Before: {tech_companies}") tech_companies[3] = "Twitter" print(f"nUpdating Microsoft with {tech_companies[3]} in the tech_companies list. ") print(f"nAfter: {tech_companies}") |
Output:
1 2 3 4 5 | Before: ['Facebook', 'Google', 'Amazon', 'Microsoft', 'IBM'] Updating Microsoft with Twitter in the tech_companies list. After: ['Facebook', 'Google', 'Amazon', 'Twitter', 'IBM'] |
Python List Looping
Within an array, a loop can print out each element that is in the array using a for loop.
Input:
1 2 | for company in tech_companies: print(f"Tech company: {company} ") |
Output:
1 2 3 4 5 | Tech company: Facebook Tech company: Google Tech company: Amazon Tech company: Twitter Tech company: IBM |
Python Array Validating Element
Python array element validation will determine if an element is in the array by using the keyword, in.
if…else Example 1
Input:
1 2 3 4 | if "Microsoft" in tech_companies: print("Yes, Microsoft is in the array.") else: print("No, Microsoft is not in the array.") |
Output:
1 | No, Microsoft is not in the array. |
if…else Example 2
Input:
1 2 3 4 | if "Facebook" in tech_companies: print("Yes, Facebook is in the array.") else: print("No, Facebook is not in the array.") |
Output:
1 | Yes, Facebook is in the array. |
Python Array Length
The internal method, len() will determine the length(element count) of an array. Notice when we updated the array we started with 0, but with the length, the count will start with 1 and not 0.
Input:
1 | print(f"The length of the array, tech_companies is: {len(tech_companies)}") |
Output:
1 | The length of the array, tech_companies is: 5 |
Python Adding Array Elements
The internal method, append() will add a specified item to the end of an array. The append() method can only take exactly one argument. In the below example, we will add 5 tech companies to the array in 5 separate append() statements.
Input:
1 2 3 4 5 6 7 8 | tech_companies.append("Microsoft") tech_companies.append("Cisco") tech_companies.append("Red Hat") tech_companies.append("Intel") tech_companies.append("Apple") print(f"Microsoft, Cisco, Red Hat, Intel and Apple are added to the array.") print(tech_companies) |
Output:
1 2 | Microsoft, Cisco, Red Hat, Intel and Apple are added to the array. ['Facebook', 'Google', 'Amazon', 'Twitter', 'IBM', 'Microsoft', 'Cisco', 'Red Hat', 'Intel', 'Apple'] |
Python Insert Item
The internal method, insert() will add a item to the index number specified.
Input:
1 2 3 4 | tech_companies.insert(1, "Samsung Electronics") print(f"Samsung Electronics is added to the array at index 1.") print(tech_companies) |
Output:
1 2 | Samsung Electronics is added to the array at index 1. ['Facebook', 'Samsung Electronics', 'Google', 'Amazon', 'Twitter', 'IBM', 'Microsoft', 'Cisco', 'Red Hat', 'Intel', 'Apple'] |
Python Item Removal
There are multiple methods to remove an item from a list. Below are the methods we will cover:
- remove()
- pop()
- del()
remove()
The method remove() will remove the specified element in the array.
Input:
1 2 3 4 | tech_companies.remove("Apple") print(f"Apple is removed from the array.") print(tech_companies) |
Output:
1 2 | Apple is removed from the array. ['Facebook', 'Samsung Electronics', 'Google', 'Amazon', 'Twitter', 'IBM', 'Microsoft', 'Cisco', 'Red Hat', 'Intel'] |
pop()
The method pop() will remove the specified element by specifying the index number. However, if the index number is not specified this method will remove the last element in the array.
Input:
1 2 3 4 | tech_companies.pop() print(f"Intel is popped from the array.") print(tech_companies) |
Output:
1 2 | Intel is popped from the array. ['Facebook', 'Samsung Electronics', 'Google', 'Amazon', 'Twitter', 'IBM', 'Microsoft', 'Cisco', 'Red Hat'] |
del
The method del will remove the specified item by the specified index number.
Input:
1 2 3 4 | del tech_companies[1] print(f"Samsung Electronics is deleted from the array.") print(tech_companies) |
Output:
1 2 | Samsung Electronics is deleted from the array. ['Facebook', 'Google', 'Amazon', 'Twitter', 'IBM', 'Microsoft', 'Cisco', 'Red Hat'] |
Python List Copying
Python list copying can be executed by using the copy() method or the list() method. In Python, copying a list by reference a variable1 to variable2 will only reference variable1.
copy() method
The copy() method produces an identical copy of the initial list.
Input:
1 2 3 4 5 | old_tech_companies = ["Facebook", "Google", "Amazon", "Microsoft", "IBM", "Cisco", "Red Hat", "Intel", "Apple"] new_tech_companies = old_tech_companies.copy() print(f"Tech companies array: {old_tech_companies} ") print(f"nNew tech companies array using the copy() method: {new_tech_companies} ") |
Output:
1 2 3 | Tech companies array: ['Facebook', 'Google', 'Amazon', 'Microsoft', 'IBM', 'Cisco', 'Red Hat', 'Intel', 'Apple'] New tech companies array using the copy() method: ['Facebook', 'Google', 'Amazon', 'Microsoft', 'IBM', 'Cisco', 'Red Hat', 'Intel', 'Apple'] |