List In Python (Complete Guide)


In this tutorial, you will learn everything about Python list in detail with various examples.

    Table Of Contents

  1. Python list introduction
  2. Creating list in python
  3. Getting size of list
  4. Access list items
  5. Add to Python List
    1. append() Method
    2. insert() Method
    3. extend() Method
  6. List slicing in Python
  7. Remove list element
    1. del statement
    2. remove() Method
    3. pop() Method
  8. List comprehension
  9. Looping through list
  10. Python list methods
  11. Conclusion

Python list introduction

List in Python is a data type that is a collection of items. It is also known as a dynamic array in other programming languages.

The list is a mutable data type. It means that you can change the value of the list items.

The items in list are separated by commas and enclosed within square brackets. Example of list: [1, 2, 3, 4, 5], [1, 2, 'a', 'b', True].

The list can contain any type of data both homogeneous or heterogeneous. It can contain integers, strings, floats, lists, tuples, dictionaries, and even other lists.

Here are some examples of a list in Python:

# empty list in python
empty_list = []

# list with integers
integers = [1, 2, 3, 4, 5]

# list with strings
strings = ['a', 'b', 'c', 'd', 'e']

# list of numbers
numbers = [1, -2, 10.5, -3.5, -4.5]

# list with mixed data types
mixed = [1, 'a', 2, 'b', True, False, [10, 20], {'name': 'John'}]

Creating list in python

You can create a list in python either by using:

  1. [ ] operator
  2. list() function

1. Create list using [ ] operator

The simplest way to create a list in python is by using the [ ] operator.

The elements of the list are separated by commas and enclosed within square brackets.

# empty list
empty_list = []
ptint(empty_list)

# list of numbers
numbers = [1, 2, 3, -5, -6]
print(numbers)

# list of strings
strings = ['a', 'b', 'c', 'd', 'e']
print(strings)

Output

[]
[1, 2, 3, -5, -6]
['a', 'b', 'c', 'd', 'e']

2. Create list using list() function

You can create a list in python by using list() function.

The list() function takes any iterable object as an argument and returns a list of the same items. The iterable object can be a list, tuple, string, set, dictionary, etc.

The list() function is useful when you want to create a list from a string or a tuple.

# using list() function
strings = list('abcde')
print(strings)

tuple1 = (1, 2, 3, 4, 5)
list1 = list(tuple1)
print(list1)

Output

['a', 'b', 'c', 'd', 'e']
[1, 2, 3, 4, 5]

Getting size of list

Working with the list you will need to know the size of the list. You can get the size of the list by using len() function.

The len() function accepts a list as an argument and returns the length of the list.

# get size of list
numbers = [1, 2, 3, 4, 5]

length = len(numbers)
print(f"Size of list: {length}")

Output

Size of list: 5

Access list items

The items in the list are ordered and can be accessed by using the index of the item.

Python follows zero-based indexing. The first item in the list is at index 0, the second item at index 1, and so on.

To access the 2nd item in the list, you can use list_name[1]. We used 1 as the index because the list starts from 0. So 1st item is at index 0, 2nd item at index 1.

# access list items
numbers = [1, 2, 3, 4, 5]

# 1st item
print(numbers[0])

# 2nd item
print(numbers[1])

# 3rd item
print(numbers[2])

Output

1
2
3

Negative Indexing

Items in the list can also be accessed using a negative index value.

The last item in the list is at index -1, the second last item at index -2, and so on.

index in python list
Index in python list

To access the last item in the list, you can use list_name[-1].

# access list items from last
numbers = [1, 2, 3, 4, 5]

# last item
print(numbers[-1])

# 2nd last item
print(numbers[-2])

Output

5
4

Adding items to list

To add item to Python list you can use the following methods:

  1. append() method
  2. insert() method
  3. extend() method
add items to python list
Methods to add items to Python list

1. append() Method

The append() method adds a new element at the end of the list.

This method is just like the push() method of the array in other languages.

The method is called with a single argument on the list you want to add the item to.

# append() method
numbers = [1, 2, 3]

numbers.append(4)  # [1, 2, 3, 4]
print(numbers)

numbers.append(5)  # [1, 2, 3, 4, 5]
print(numbers)

Output

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

2. insert() Method

The insert() method inserts an element at the specified position.

It accepts two arguments, the index of the element before which the new element is to be inserted and the element to be inserted.

list_name.insert(index, element)

Here is an example to show how to use the insert() method.

# insert() method
numbers = [1, 2, 3]

# insert 4 at position 2
# position 2 = index 1
numbers.insert(1, 4)  # [1, 4, 2, 3]
print(numbers)

# insert 'hello' at position 3
numbers.insert(2, 'hello')  # [1, 4, 'hello', 2, 3]
print(numbers)

Output

[1, 4, 2, 3]
[1, 4, 'hello', 2, 3]

3. extend() Method

The extend() method is bit different from append() method. It accepts an iterable object as an argument and adds the items of the iterable object to the list.

Every item in the iterable object will become a new item in the list.

# extend() method
list1 = [1, 2, 3]
list2 = [4, 5, 6]

list1.extend(list2)  # [1, 2, 3, 4, 5, 6]
print(list1)

If you pass a string as an argument in the extend() method, it will be converted to a list of characters.

# passing string
list1 = []
list2 = 'hello'

list1.extend(list2)  # ['h', 'e', 'l', 'l', 'o']
print(list1)

Output

['h', 'e', 'l', 'l', 'o']

List Slicing In Python

List slicing is a way to extract a portion of a list and assign it to a new list.

It is done by using the syntax list_name[start:end]. Where start is the index of the first item to be included in the new list and end is the index of the item after the last item to be included in the new list.

If you omit the start index, it is assumed to be 0. If you omit the end index, it is assumed to be the end of the list.

list slicing in python example
List slicing in Python

The following example shows how to use list slicing in Python.

# list slicing
str = ['a', 'b', 'c', 'd', 'e', 'f']

list1 = str[1:4]  # ['b', 'c', 'd']
print(list1)

list2 = str[:3]  # ['a', 'b', 'c']
print(list2)

list3 = str[2:]  # ['c', 'd', 'e', 'f']
print(list3)

list4 = str[:]  # ['a', 'b', 'c', 'd', 'e', 'f']
print(list4)

Output

['b', 'c', 'd']
['a', 'b', 'c']
['c', 'd', 'e', 'f']
['a', 'b', 'c', 'd', 'e', 'f']

Removing items from list

There are 3 ways to remove items from a Python list.

  1. remove() Method
  2. pop() Method
  3. del statement
delete items to python list
Methods to delete items from Python list

1. remove() Method

The remove() method is used to remove an element from a list using its value.

To remove an element from the list pass the element value as an argument and the first matching element will be removed.

# remove() method
str = ['a', 'b', 'c', 'd', 'e']

str.remove('c')  # ['a', 'b', 'd', 'e']
print(str)

str.remove('a')  # ['b', 'd', 'e']
print(str)

Output

['a', 'b', 'd', 'e']
['b', 'd', 'e']

2. pop() Method

The pop() method removes an element from a list using the index.

If you omit the index, it will remove the last element from the list.

If you pass an index, it will remove the element at that index.

# pop() method
str = ['a', 'b', 'c', 'd', 'e']

str.pop()  # ['a', 'b', 'c', 'd']
print(str)

str.pop(2)  # ['a', 'b', 'd']
print(str)

Output

['a', 'b', 'c', 'd']
['a', 'b', 'd']

3. del Statement

The del statement is used to delete one or more elements from a list.

To delete a single element from a list, you can use the syntax del list_name[index].

To delete multiple elements from a list, you can use the syntax del list_name[start:end].

# del statement
numbers = [1, 2, 3, 4, 5]

del numbers[2]  # [1, 2, 4, 5]
print(numbers)

del numbers[1:3]  # [1, 5]
print(numbers)

Output

[1, 2, 4, 5]
[1, 5]

List Comprehension

List comprehension is a Python feature that allows you to create a new list by applying some logic to each element of another list.

The syntax of list comprehension is [expression for item in list_name].

The expression is the expression that will be applied to each element of the list.

# list comprehension
numbers = [1, 2, 3, 4, 5]

new_list = [x * 2 for x in numbers]  # [2, 4, 6, 8, 10]
print(new_list)

even_list = [x for x in numbers if x % 2 == 0]  # [2, 4, 6]
print(even_list)

Output

[2, 4, 6, 8, 10]
[2, 4, 6]

Stay Ahead, Learn More


Looping Through List

There could be many ways to loop through a list in Python. Python for loop is most commonly used to loop through a list.

# for loop in list
numbers = [1, 2, 3, 4, 5]

for number in numbers:
      print(number, end=" ")

Output

1 2 3 4 5

Another way is to use the range() function over the length of the list and use for loop to loop through the list.

To access the element of the list, you can use the index of the element.

# for loop with range
numbers = [1, 2, 3, 4, 5]

for index in range(len(numbers)):
      print(numbers[index], end=" ")

Output

1 2 3 4 5

Python List Methods

Python list has many methods that are used to manipulate the list.

The Python list methods are as follows:

MethodDescription
append()The append() method is used to add an element at the end of the list.
clear()The clear() method is used to remove all the elements from the list.
copy()The copy() method is used to return a shallow copy of the list.
count()The count() method is used to count the number of elements in the list.
extend()The extend() method is used to add all the elements of a list to the list.
index()The index() method is used to return the index of the first element in the list.
insert()The insert() method is used to add an element at the specified position in the list.
pop()The pop() method is used to remove an element from the list.
remove()The remove() method is used to remove the first occurrence of an element from the list.
reverse()The reverse() method is used to reverse the order of the list.
sort()The sort() method is used to sort the list.

Reference Python list docs.