Python Arrays

  • Post author:
  • Post comments:0 Comments
  • Reading time:65 mins read

In Python, arrays are created using the list data type. A list is a collection of items that are ordered and changeable. Lists are written with square brackets and the items are separated by commas.

Here is an example of a list in Python:


my_list = [1, 2, 3, 4]

You can access the items on a list using the index number of the item in the list. The index numbers start at 0 for the first item.


# Access the first item in the list

first_item = my_list[0]

print(first_item) # Output: 1


# Access the third item in the list

third_item = my_list[2]

print(third_item) # Output: 3

You can also use negative index numbers to access items from the end of the list. For example, the index number -1 refers to the last item in the list, -2 refers to the second last item, and so on.


# Access the last item in the list

last_item = my_list[-1]

print(last_item) # Output: 4


# Access the second last item in the list

second_last_item = my_list[-2]

print(second_last_item) # Output: 3

You can also use index numbers to modify the value of an item in a list.


# Modify the value of the first item in the list

my_list[0] = 10

print(my_list) # Output: [10, 2, 3, 4]

You can use the len() function to find the length of a list.


# Find the length of the list

list_length = len(my_list)

print(list_length) # Output: 4

You can use the append() method to add a new item to the end of a list.


# Add a new item to the end of the list

my_list.append(5)

print(my_list) # Output: [10, 2, 3, 4, 5]

You can use the insert() method to add a new item at a specific index in the list.


# Add a new item at index 1

my_list.insert(1, 0)

print(my_list) # Output: [10, 0, 2, 3, 4, 5]

You can use the remove() method to remove an item from the list.


# Remove the item at index 2

my_list.remove(2)

print(my_list) # Output: [10, 0, 3, 4, 5]

You can use the pop() method to remove and return the last item from the list.


# Remove and return the last item from the list

last_item = my_list.pop()

print(last_item) # Output: 5

print(my_list) # Output: [10, 0, 3, 4]

You can use the clear() method to remove all items from the list.


# Remove all items from the list

my_list.clear()

print(my_list) # Output: []

You can use the index() method to find the index of an item in the list.


my_list = [10, 3, 4]

# Find the index of the item 4

index_of_item_4 = my_list.index(4)

print(index_of_item_4) # Output: 2

You can use the count() method to count the number of occurrences of an item in the list.


# Count the number of occurrences of the item 10

count_of_item_10 = my_list.count(10)

print(count_of_item_10) # Output: 1

You can use the sort() method to sort the items of the list in ascending order.


my_list = [10, 3, 4, 5, 1]

# Sort the list in ascending order

my_list.sort()

print(my_list) # Output: [1, 3, 4, 5, 10]

You can also use the reverse() method to reverse the order of the items in the list.


# Reverse the order of the items in the list

my_list.reverse()

print(my_list) # Output: [10, 5, 4, 3, 1]

Table of array methods in Python

MethodDescription
append()Add an item to the end of the list
clear()Remove all items from the list
copy()Return a copy of the list
count()Return the number of occurrences of an item in the list
extend()Add all items from an iterable to the end of the list
index()Return the index of the first occurrence of an item in the list
insert()Insert an item at a specific index in the list
pop()Remove and return the item at a specific index in the list (default is the last item)
remove()Remove the first occurrence of an item from the list
reverse()Reverse the order of the items in the list
sort()Sort the items of the list in ascending order
Publisher

Publisher

Publisher @ideasorblogs

Leave a Reply