Python List

  • Python

Just like Arrays in other programming languages, Python list has a similar structure. It is used to store multiple values which is not possible with a variable. Python lists can contain values of different types like numbers, strings, and even another list.

A list is denoted by square brackets [] and can contain any number of values. Let’s understand this by looking at a few examples.

Creating a List

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

# Empty List
new_list = []

mixed_list = [1, 'a', 2, 'b']

A list is created by placing all the values within the square bracket and each value should be separated with a comma. Now if we print this, we get output as :

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[]
[1, 'a', 2, 'b']

We can also create a list within a list as mentioned earlier and this type of list is called a nested list.

nested_list = [1, 'b', 3, [4, 'd', 'f']]

Accessing items/values from a list

Accessing a specific item from a list is very easy and there are two different ways to do this, Indexing and Negative Indexing.

Indexing

Indices in Python start from 0 which means if a list contains 4 items, index for this list will be from 0 to 3 and to access these items we’ll use the index operator []. Check out some examples :

new_list = ['t', 'e', 'c', 'h', 'i', 'e']

print(new_list[0])

print(new_list[1])

print(new_list[2])

print(new_list[3])

print(new_list[4])

print(new_list[5])

Output :

t
e
c
h
i
e

In case you try to access an item outside the length of the list, you’ll get an error something like this –

>>> new_list = ['t', 'e', 'c', 'h', 'i', 'e']
>>> new_list[6]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Negative Indexing

Just like in Indexing, indices start from 0, indices in negative indexing start from -1 but from the end.

List Indexing

Let’s consider the same example for this and see how it works.

new_list = ['t', 'e', 'c', 'h', 'i', 'e']

print(new_list[-1])

print(new_list[-2])

print(new_list[-3])

print(new_list[-4])

print(new_list[-5])

print(new_list[-6])

Output :

e
i
h
c
e
t

we were successfully able to print values at a specific location. Now, what if we want to print values from one point to another. Suppose, we want to print from [1] to [3]. How do we do that? Here we use slicing which is a method to print values ranging from one point to another in a list.


Slicing in Lists

Slicing is a method to access a range of values in a list and we use a colon : to achieve this.

new_list = ['t', 'e', 'c', 'h', 'i', 'e']

#printing all the values
print(new_list[:])

#printing values from 2nd position to end
print(new_list[1:])

#printing from beginning to 4th value
new_list[:-2]

Output :

['t', 'e', 'c', 'h', 'i', 'e']
['e', 'c', 'h', 'i', 'e']
['t', 'e', 'c', 'h']

What if we want to print all elements at odd placed or maybe at even places in a list. In this scenario, we use step size in the list. The format for such slicing will be new_list[start:end:step].

Example :

new_list = ['t', 'e', 'c', 'h', 'i', 'e']

# Printing all the elements at even places
print(new_list[::2])

# Printing all the elements at even places
print(new_list[1::2])

Output :

['e', 'h', 'e']
['t', 'c', 'i']

Adding or Deleting Elements from a list

Python lists are mutable which means that we can perform certain kinds of operations such as add, delete which cannot be done with tuples or strings.

Let’s understand this by following some examples.

Adding Elements

new_list = ['t', 'e', 'c', 'h', 'i', 'e']

# Adding element to the end of the list
new_list.append('h')
print(new_list)

# Changing an element at a place
new_list[0] = d
print(new_list)

# Adding multiple elements
new_list.extend(['o', 'u', 'r', 's'])
print(new_list)

# Adding element at a specific position using insert. (position, value)
new_list.insert(1, 'h')
print(new_list)

Output:

['t', 'e', 'c', 'h', 'i', 'e', 'h']
['d', 'e', 'c', 'h', 'i', 'e', 'h']
['d', 'e', 'c', 'h', 'i', 'e', 'h', 'o', 'u', 'r', 's']
['d', 'h', 'e', 'c', 'h', 'i', 'e', 'h', 'o', 'u', 'r', 's']

Deleting/Removing elements

We can delete/remove elements using remove(), pop() or delete() methods.

new_list = ['t', 'e', 'c', 'h', 'i', 'e']

# Pop will remove the last element in the list if we do not specify the location
new_list.pop()
print(new_list)

# Remove a specific value
new_list.remove('t')
print(new_list)

# Deleting a range of values or a specific value
del new_list[1:]
print(new_list)

Output :

['t', 'e', 'c', 'h', 'i']
['e', 'c', 'h', 'i']
['e']

Python list methods

The examples we saw above for adding or removing elements were performed using list methods. Apart from this, there are more list methods that you can try. You can access the methods by typing listname.method_name(). Type the list name with a . and press TAB to access the methods.

List method Examples

  • append()
  • clear()
  • copy()
  • count()
  • extend()
  • index()
  • pop()
  • insert()
  • reverse()
  • remove()
  • sort()
  • sum()