Python Lists Examples

Python Lists with Examples

In this tutorial you will learn everything about Python Lists, like how the lists are created in Python, accessing list elements, slicing of the list, add or change elements from the lists and delete elements from the lists.

Python Lists with Examples

Python Lists Examples

Lists are ordered sequences that can hold different object types. They use “[]” square brackets and commas to separate objects in the list.

It supports indexing and slicing. Lists can also ne nested and has variety of useful methods to manage them.

For example,

[a,b,c,d,e]

Create a list in Python

Let’s see how to create a list in Python. In Python as mentioned above list can be created by placing all the elements inside the square bracket “[]”, separated by commas. Lists can have any number of elements in it and they could be of different data types like string, integer, float etc.,

Here are some examples :

#list of mixed data type
>>> my_list = ['HELLO', 10, 20.5]

>>> my_list
['HELLO', 10, 20.5]

#list of integers
>>> num_list = [1,2,3]

#nested list
>>> my_list = ['HELLO', [1,2,3], ['a']]

How to access list elements in Python ?

There are various ways using which you can access elements of lists in Python.

Firstly, we can use the list index to access an element from the list. In Python the indices starts from zero (0). Note, you will get IndexError if you try to access invalid index outside the range.

For example,

>>> my_list = ['apple','boy','cat']

>>> my_list[0]
'apple'

>>> my_list[2]
'cat'

#Nested list
>>> nested_list = ["HELLO", ['a','b','c']]

>>> nested_list[0][1]
'E'

>>> nested_list[1][1]
'b'

>>> nested_list[5][1]
IndexError  Traceback (most recent call last)
<ipython-input-177-fc75226bc9dd> in <module>
----> 1 nested_list[5][1]

IndexError: list index out of range

Note, you can also use negative indexing to access the elements from list as shown below.

>>> mylist = ['apple','boy','cat']

>>> mylist[-3]
'apple'

>>> mylist[-1]
'cat'

Slicing lists in Python

In this section you will learn about usage of slice operator in Python lists similar to Python strings slicing.

Here are some examples for lists slices in Python.

>>> mylist = ['apple','boy','cat', 'dog', 'elephant']

#elements from 2nd to 5th
>>> mylist[1:]
['boy', 'cat', 'dog', 'elephant']

#element from 2nd to 3rd
>>> mylist[1:3]
['boy', 'cat']

#elements from begining to third
>>> mylist[:-2]
['apple', 'boy', 'cat']

#all elements
>>> mylist[:]
['apple', 'boy', 'cat', 'dog', 'elephant']

Add/Modify elements in the lists

Lists in Python are mutable unlike string or tuple. Therefore, you can add or change lists at any point of time. You need to use list indexing and equal “=” operator to modify any element in the list.

Here are some examples on how to add/ modify list elements.

#lets try to fix the first element in the list
>>> mylist = ['pple','boy','cat', 'dog', 'elephant']

#modify first element in the list
>>> mylist[0] = 'apple'

#check 
>>> mylist
['apple', 'boy', 'cat', 'dog', 'elephant']

#add element to list using append() method
>>> mylist.append('frog')

>>> mylist
['apple', 'boy', 'cat', 'dog', 'elephant', 'frog']

#another way to add - concatenating lists
>>> another_list = ['goat', 'hen']

>>> newlist = mylist + another_list

>>> newlist
['apple', 'boy', 'cat', 'dog', 'elephant', 'frog', 'goat', 'hen']

Delete or Remove lists elements in Python

You can remove the specific element from the list using pop() method using list index.

If you use pop() method without specifying any index, then it will remove the last element from the list.

Here are some examples for pop() method:

>>> mylist = ['apple', 'boy', 'cat', 'dog', 'elephant', 'frog']

>>> mylist.pop()
'frog'

>>> mylist
['apple', 'boy', 'cat', 'dog', 'elephant']

>>> mylist.pop(3)
'dog'

>>> mylist
['apple', 'boy', 'cat', 'elephant']

You can also use del, remove and clear methods as shown below.

#del examples
>>> mylist = ['apple', 'boy', 'cat', 'dog', 'elephant', 'frog']

#delete specific item
>>> del mylist[5]

>>> mylist
['apple', 'boy', 'cat', 'dog', 'elephant']

>>> mylist = ['apple', 'boy', 'cat', 'dog', 'elephant', 'frog']

#delete multiple items
>>> del mylist[1:3]

>>> mylist
['apple', 'dog', 'elephant', 'frog']

>>> mylist = ['apple', 'boy', 'cat', 'dog', 'elephant', 'frog']

#delete entire list
>>> del mylist

Here are examples for remove and clear methods.

>>> mylist = ['apple', 'boy', 'cat', 'dog', 'elephant', 'frog']

#remove example
>>> mylist.remove('frog')
['apple', 'boy', 'cat', 'dog', 'elephant']

#clear example
>>> mylist.clear()

>>> mylist
[]

That’s it you had learnt all about lists in Python. Hope it helped 🙂

You’ll also like:

References

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments