Python

Python Lists

A list is a built-in Python data type used to store multiple values in a single variable.

Lists are one of the most commonly used data structures in Python because they allow us to store and manage collections of related data.

For example, instead of creating separate variables:

fruit1 = "apple"
fruit2 = "mango"
fruit3 = "papaya"
fruit4 = "cherry"

we can store all the values in a single list:

fruits = ["apple", "mango", "papaya", "cherry"]

Important Characteristics of Lists

Python lists are:

  • Ordered — elements maintain their position.
  • Mutable — elements can be changed after the list is created.
  • Indexed — each element has an index number.
  • Allow duplicates — the same value can appear multiple times.
  • Flexible — a list can contain different data types.

1. Creating a List

A list is created using square brackets [].

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

print(fruits)

Output

['apple', 'mango', 'papaya', 'cherry', 'grapes', 'kiwi']

A list can contain strings, numbers, Boolean values, and even other lists.

Example

data = [10, 20.5, "Python", True]

print(data)

Output

[10, 20.5, 'Python', True]

2. Finding the Length of a List

The len() function returns the number of elements present in a list.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

print(len(fruits))

Output

6

There are six elements in the list.

Important

len() counts elements, not the index numbers.

For example:

Index:   0       1        2        3        4        5
Value: apple   mango   papaya   cherry   grapes    kiwi

The list contains 6 elements, but the indexes go from 0 to 5.

3. Checking the Data Type

You can use the type() function to find the data type of a variable.

Example

fruits = ["apple", "mango", "papaya"]

print(type(fruits))

Output

<class 'list'>

This confirms that fruits is a Python list.

4. Creating a List Using list()

Python also provides the list() constructor for creating lists.

Example

list1 = list(("apple", "mango", "papaya", "cherry"))

print(list1)

Output

['apple', 'mango', 'papaya', 'cherry']

Here, the list() function converts the tuple into a list.

5. Accessing List Elements

Each element in a list has an index number.

Python uses zero-based indexing, which means the first element has index 0.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

print(fruits[0])
print(fruits[1])
print(fruits[2])

Output

apple
mango
papaya

The index positions are:

Index:   0        1        2         3        4       5
Value: apple    mango    papaya    cherry   grapes   kiwi

6. Accessing Characters Inside a List Element

If an element is a string, you can use another index to access an individual character.

Example

fruits = ["apple", "mango", "papaya", "cherry"]

print(fruits[1])
print(fruits[1][0])
print(fruits[2][1])

Output

mango
m
a

Explanation

fruits[1]

returns:

mango

Then:

fruits[1][0]

gets the first character of "mango":

m

Similarly:

fruits[2][1]

gets the character at index 1 from "papaya":

a

7. List Slicing

List slicing allows you to extract multiple elements from a list.

Syntax

list[start:end]

The starting index is included, but the ending index is excluded.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

print(fruits[2:5])

Output

['papaya', 'cherry', 'grapes']

Indexes 2, 3, and 4 are selected.

Leaving Out the End Index

print(fruits[2:])

This means:

  • Start at index 2 and continue to the end.

Output

['papaya', 'cherry', 'grapes', 'kiwi']

Leaving Out the Starting Index

print(fruits[:4])

This means:

  • Start from the beginning and stop before index 4.

Output

['apple', 'mango', 'papaya', 'cherry']

8. Negative Indexing

Python allows you to access list elements from the end using negative indexes.

The last element has index -1.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

print(fruits[-1])

Output

kiwi

The index positions are:

Positive:  0       1        2        3        4       5
Value:    apple   mango   papaya   cherry   grapes   kiwi
Negative: -6      -5       -4       -3       -2      -1

Negative Slicing

print(fruits[-4:-1])

Output

['papaya', 'cherry', 'grapes']

The ending index -1 is not included.

9. Changing List Elements

Lists are mutable, meaning their elements can be changed after the list is created.

You can change an element by referring to its index.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

fruits[3] = "pear"

print(fruits)

Output

['apple', 'mango', 'papaya', 'pear', 'grapes', 'kiwi']

The element at index 3 was changed from "cherry" to "pear".

10. Changing Multiple List Elements

You can replace multiple elements using slicing.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

fruits[2:4] = ["guava", "banana"]

print(fruits)

Output

['apple', 'mango', 'guava', 'banana', 'grapes', 'kiwi']

Indexes 2 and 3 were replaced.

11. Adding Elements Using append()

The append() method adds one element to the end of a list.

Example

fruits = ["apple", "mango", "papaya", "cherry"]

fruits.append("banana")

print(fruits)

Output

['apple', 'mango', 'papaya', 'cherry', 'banana']

12. Adding Elements Using insert()

The insert() method adds an element at a specific position.

Syntax

list.insert(index, value)

Example

fruits = ["apple", "mango", "papaya", "cherry"]

fruits.insert(0, "kiwi")

print(fruits)

Output

['kiwi', 'apple', 'mango', 'papaya', 'cherry']

The value "kiwi" was inserted at index 0.

13. Adding Multiple Elements Using extend()

The extend() method adds all elements from another iterable, such as another list, to the end of the current list.

Example

x = ["apple", "banana", "cherry"]
y = ["mango", "pineapple", "papaya"]

x.extend(y)

print(x)

Output

['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']

append() vs extend()

This difference is important.

append()

x = ["apple", "banana"]

x.append(["mango", "papaya"])

print(x)

Output:

['apple', 'banana', ['mango', 'papaya']]

The entire list is added as one element.

extend()

x = ["apple", "banana"]

x.extend(["mango", "papaya"])

print(x)

Output:

['apple', 'banana', 'mango', 'papaya']

The individual elements are added to the list.

14. Removing an Element Using remove()

The remove() method removes the first matching value from a list.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "papaya", "kiwi"]

fruits.remove("papaya")

print(fruits)

Output

['apple', 'mango', 'cherry', 'grapes', 'papaya', 'kiwi']

There were two "papaya" values, but remove() removed only the first occurrence.

15. Removing an Element Using pop()

The pop() method removes an element using its index and returns the removed value.

If you don't provide an index, pop() removes the last element.

Example

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

fruits.pop()

print(fruits)

Output

['apple', 'mango', 'papaya', 'cherry', 'grapes']

The last element, "kiwi", was removed.

Removing a Specific Element with pop()

fruits = ["apple", "mango", "papaya", "cherry", "grapes", "kiwi"]

fruits.pop(3)

print(fruits)

Output

['apple', 'mango', 'papaya', 'grapes', 'kiwi']

The element at index 3, "cherry", was removed.

16. remove() vs pop()

MethodRemoves ByExample
remove()Valuefruits.remove("apple")
pop()Indexfruits.pop(2)
pop()Last element if no indexfruits.pop()

17. Removing All Elements Using clear()

The clear() method removes all elements from a list.

The list itself still exists; it simply becomes empty.

Example

list1 = [1, 2, 3, 4, 5, 20.5, "hello", True]

print(list1)

list1.clear()

print(list1)

Output

[1, 2, 3, 4, 5, 20.5, 'hello', True]
[]

18. Deleting a List Using del

The del statement can delete an entire list or delete specific elements.

Delete the entire list

list2 = [1, 2, 3, 4, 5, 20.5, "hello", True]

del list2

After this statement, the variable list2 no longer exists.

Therefore, this would produce an error:

print(list2)

because the list has been deleted.

Delete a Specific Element

You can also use del with an index.

fruits = ["apple", "mango", "papaya", "cherry"]

del fruits[1]

print(fruits)

Output

['apple', 'papaya', 'cherry']

19. Sorting a List

The sort() method sorts the elements of a list in ascending order by default.

Example

list3 = ["apple", "papaya", "mango", "cherry"]

list3.sort()

print(list3)

Output

['apple', 'cherry', 'mango', 'papaya']

For strings, sorting is based on alphabetical/lexicographical order.

20. Sorting in Descending Order

You can use:

sort(reverse=True)

to sort the list in descending order.

Example

list3 = ["apple", "papaya", "mango", "cherry"]

list3.sort(reverse=True)

print(list3)

Output

['papaya', 'mango', 'cherry', 'apple']

21. Sorting Numbers

The sort() method also works with numbers.

Example

x = [1, 100, 20, 60, 50]

x.sort()

print(x)

Output

[1, 20, 50, 60, 100]

22. Reversing a List

The reverse() method reverses the current order of elements.

It does not sort the list.

Example

list3 = ["apple", "papaya", "mango", "cherry"]

list3.reverse()

print(list3)

Output

['cherry', 'mango', 'papaya', 'apple']

Important Difference

sort():

  • Arranges elements according to their values.

reverse():

  • Reverses the current order of elements.

For example:

numbers = [50, 10, 30, 20]

numbers.reverse()

print(numbers)

Output:

[20, 30, 10, 50]

The numbers were not sorted. Their original order was simply reversed.

23. Important List Methods

MethodDescription
append()Adds one element to the end
insert()Adds an element at a specific position
extend()Adds elements from another iterable
remove()Removes the first matching value
pop()Removes an element by index
clear()Removes all elements
sort()Sorts the list
reverse()Reverses the current order

 

Interactive Sandbox
Python