Python List with Example – Python Tutorial

1 year ago Lalit Bhagtani 0

In Python list are a sequence of objects just like a string. Unlike strings, list is a mutable data structure, which means that data in the list can be changed or replaced.

In this tutorial, we will learn about the following:

  1. Creating List
  2. List Indexing
  3. List Slicing
  4. Nesting List
  5. Update & Delete List Element

Python List

Creating List

In Python list are created by enclosing sequence of elements separated by commas inside brackets “[ ]“. For example:

# creating list with same data type
 l = [1,2,3,4,5] 

# creating list with different data type
 l = [1,'Jan',2.0,True]

# creating empty list
 l = []

List Indexing

In python list are a sequence just like a string, So we can use indexes to access individual element of the list. Index starts from 0 and brackets [] are used after an object variable to access the element. For example:
[datacamp_exercise lang=”python” height=”200″ min-height=”100″ max-height=”300″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# create list object and assign it to variable l
l = [1,2,3,4,5]

# accessing element at index 0
print(l[0])

# accessing element at index 1
print(l[1])

# accessing last element of the list
print(l[2])

# another way of accessing last element of the list
print(l[-1])
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]

[/datacamp_exercise]
Like string, negative numbers can also be used for indexing. Indexing diagram for list [‘a’,’b’,’c’,’d’] as follows: 

  a   b  c  d
  0   1  2  3
 -4  -3 -2 -1   

List Slicing

In python list can be sliced by using colon “:” operator inside brackets “[]“. 

        Syntax :  [ start index : end index : step ]
                      start index : slice of the given list will starts from this index, including element at start index
                      end index : slice of the given list will end at this index, excluding element at end index
                      step : it is a frequency of grabbing elements from the given list

For example :
[datacamp_exercise lang=”python” height=”400″ min-height=”100″ max-height=”500″]
[datacamp_sample_code]
# create list object and assign it to variable l
l = (‘a’,’b’,’c’,’d’)

print(l[1:])
# This prints a slice of the list, starting from index 1 and ending at last index

print(l[:1])
# This prints a slice of the list, starting from index 0 and ending at index 1

print(l[1:2])
# This prints a slice of the list, starting from index 1 and ending at index 2

print(l[::2])
# This prints a slice of the list, starting from index 0 and ending at last index and grab every alternate element only

# print complete list in reverse
print(l[::-1])

[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

Nesting List

In python list can be created inside another list. This can be used to create matrix data structure. For example:
[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create three list object and assign it to variable l1, l2, l3
l1 = [1,2,3,4]
l2 = [5,6,7,8]
l3 = [9,10,11,12]

# create a list of list to make a matrix
m = [l1,l2,l3]

# print 2nd list
print(m[1])

# print 1st element of 3rd list
print(m[2][0])
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

Update & Delete List Element

Updating list element is very simple, just use assignment operator to assign new value to position of the element, which you want to update. 

For deleting list element or list object entirely, we can use del statement. For example:
[datacamp_exercise lang=”python” height=”400″ min-height=”200″ max-height=”500″]
[datacamp_sample_code]
# create list object and assign it to variable l
l = [1,2,3,4,5,6,7,8]

# print all elements of list
print(l)

# update element at index 1
l[1] = 9

# print all elements of list
print(l)

# delete element at index 2
del l[2]

# check list elements after deletion
print(l)

# delete element from index 4 to 7
del l[4:7]

# check list elements after deletion
print(l)

# delete list
del l

# check list
print(l)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

References :-

  1. List Docs

That’s all for Python List with Example. If you liked it, please share your thoughts in comments section and share it with others too.

Next