List in Python

In Python, a list is an ordered collection of data or elements which are mutable, meaning you can add, remove, or modify elements from the list after they are created. Lists are defined by enclosing a comma-separated sequence of elements within square brackets [ ].

Here's an example of a list that initializes a list called fruits containing five fruit strings.

fruits = ["mango", "grapes", "guava", "apple", "watermelon"]

Creating a List

In Python, a list can be created by enclosing elements in square brackets:

Example:

subjects = ["Python", "Java", "MySQL", "MongoDB", "Oracle"]
nums = [20, 77, 33, 45, 10, 40, 56]

print(subjects)
print(nums)

Output:

['Python', 'Java', 'MySQL', 'MongoDB', 'Oracle']
[20, 77, 33, 45, 10, 40, 56]

In Python, you can also create a new list using the list() constructor.

Example:

colors = list(("green", "blue", "green", "white"))
print(colors)

Output:

['green', 'blue', 'green', 'white']

Accessing Elements

Elements in a list can be accessed by their index number, starting from 0 for the first element.

Example:

fruits = ["mango", "grapes", "guava", "apple", "watermelon"]
print("Item at index 0 = "+ fruits[0])
print("Item at index 1 = "+ fruits[1])
print("Item at index 2 = "+ fruits[2])
print("Item at index 3 = "+ fruits[3])
print("Item at index 4 = "+ fruits[4])

Output:

Item at index 0 = mango
Item at index 1 = grapes
Item at index 2 = guava
Item at index 3 = apple
Item at index 4 = watermelon

Accessing Elements using Loop

You can access elements of a list using a for loop.

Example:

colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
for c in colors:
   print(c)

Output:

blue
white
red
green
orange
purple
brown

You can also loop through a list of elements using a for loop with the range() function.

Example:

colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
for i in range(len(colors)):
   print(colors[i])

Output:

blue
white
red
green
orange
purple
brown

Accessing Elements using Slicing

Items in a list can also be accessed using slicing, which involves specifying a range of indices – the start index and the end index separated by a colon.

Here's  an example of accessing elements starting from index 0 to 3:

colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
print(colors[0:3])

In the above code, the first line initializes a list called colors containing seven color strings. The second line, colors[0:3] uses list slicing to access a subset of elements from the colors list. 0 is the starting index, which corresponds to the first element in the list, "blue" and 3 is the ending index, which is one position past the last element you want to include. In this case, it's the element at index 2, which is "red".

The output of the above code is as follows:

['blue', 'white', 'red']

If you do not specify the end index, it will read till the last index.

Example:

colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
print(colors[4:])

Output:

['orange', 'purple', 'brown']

Adding Elements

To add items to a list, you can use the append() function that will add item at the end of the list items.

Here's an example of adding two elements 100 and 200 to a list:

nums = [12, 33, 45, 77, 23]
nums.append(100)
nums.append(200)
print(nums)

The output of the above code is as follows:

[12, 33, 45, 77, 23, 100, 200]

Inserting Elements at a Specified Index

The insert() function is used to add elements at a specific index within a list.

The syntax is insert(index, item).

Here's an example of inserting the number 88 at index 2 of the list:

nums = [1, 2, 3, 4, 5]
nums.insert(2, 88)
print(nums)

The output of the above code is as follows:

[1, 2, 88, 3, 4, 5]

Removing Elements

To remove an item from a list, you can use the remove() function.

The syntax is remove(item).

Here's an example of removing the "carrot" element from the list:

fruits = ["apple", "mango", "carrot", "grapes", "orange"]
fruits.remove("carrot")
print(fruits)

The output of the above code is as follows:

['apple', 'mango', 'grapes', 'orange']

Removing Elements by Index

In Python, you can remove elements from a list by their index using the pop() function.

The syntax is pop(index).

Here's an example of removing an element at index 2:

fruits = ["apple", "mango", "carrot", "grapes", "orange"]
fruits.pop(2)

The output of the above code is as follows:

['apple', 'mango', 'grapes', 'orange']

If you do not specify the index number, the pop() function will remove the last item from the list.

Example:

fruits = ["apple", "mango", "carrot", "grapes", "orange"]
fruits.pop()
print(fruits)

Output:

['apple', 'mango', 'carrot', 'grapes']

Deleting Elements

In Python, you can use del statement to remove elements from a list by specifying the index of the element you want to delete.

Example:

fruits = ["mango", "grapes", "guava", "apple", "watermelon"]
del fruits[3]  # deletes the element at index 3
print(fruits)

Output:

['mango', 'grapes', 'guava', 'watermelon']-

Modifying Elements

You can update elements at any index of a list.

Here's an example of updating an element at index 2:

colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
colors[2] = "pink"
print(colors)

The output of the above code is as follows:

['blue', 'white', 'pink', 'green', 'orange', 'purple', 'brown']

List Length

In Python, the len() function is used to find the length or size of a list.

Example:

colors = ["blue", "white", "red", "green", "orange", "purple", "brown"]
print(len(colors))

Output:

7

Concatenating Lists

In Python, you can combine two or more list using the + operator.

Example:

list1 = ["one", "two", "three", "four"]
list2 = ["seven", "eight", "nine"]
new_list = list1 + list2
print(new_list)

Output:

['one', 'two', 'three', 'four', 'seven', 'eight', 'nine']

Nesting Lists

In Python, you can create lists of lists, allowing for more complex data structures:

Example:

nested_list = [[1, 2, 3, 4], [5, 6, 7, 8, 9]]
print(nested_list)

Output:

[[1, 2, 3, 4], [5, 6, 7, 8, 9]]