Python – Lists

After reading this Python lists topic, you will understand how to create the list, and you will know how to accessing Items, changing Items and use lists built-in methods in Python.

A list is a variable that collects same or different type of elements(items) in a sequence. The elements of the list can be changed.

How to create List in python?

The list items should be written within the square brackets (i.e. [ ]) and each item separated by a comma (,).

Example:

Program (1): To demonstrate how to create and display list in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# print a list
print(x)

Output(1)

[2, 3, 4]

Accessing Items from List

Example:

Program (1): To demonstrate how to access list items in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
print(x[0])
print(x[1])

Output(1)

2
3

Explanation: for Program(1) and Output(1)

  • statement print(x[0]) gives output 2 describe the first item of the list, associated with index number 0.
  • statement print(x[1]) gives output 3 describe the second item of the list, associated with index number 1.

Changing Items from List

Example:

Program (1): To demonstrate how to change items from the list in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
#print original list
print(x)
#change second item of list
x[1]=5
# print list with changed items
print(x)

Output(1)

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

Using loop over the List

Using loop over the list means we move through the list and visit each item of the list.

Example:

Program (1): To demonstrate how to use a loop over the list to access list items in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# use for loop in order to print each item of the list
for i in x:
print(i)

Output(1)

2
3
4

List handling built-in methods

Some of commonly used list handling methods like len(), insert(), remove(), pop() and clear().

len() : This method gives the total number of items in the list.

Example:

Program (1): To demonstrate how to use len() function over the list in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# using len() method to calculate to the total number of items in the list.
print(len(x))

Output(1)

3

insert(): This method inserts an item at the particular position defined by the index number of the list.

Example:

Program (1): To demonstrate how to use insert() function on the list in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# using insert() method to add item 5 in the second position of the list (for second position index number is 1)
x.insert(1,5)
# print list
print(x)

Output(1)

[2, 5, 3, 4]

remove() : This method remove an item from the list.

Example:

Program (1): To demonstrate how to use remove() function on the list in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# using remove() method to remove item 3 from the list
x.remove(3)
# print list
print(x)

Output(1)

[2, 4]

pop(): This method removes and returns an item from the particular position defined by the index number of the list. When index number is not specified then the last item of the list gets removed.

Example:

Program (1): To demonstrate how to use pop() function on the list in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# using pop() method to remove last item from the list.
x.pop ()
# print list
print(x)

Output(1)

[2, 3]

clear() : This method remove all items from the list.

Example:

Program (1): To demonstrate how to use clear() function on the list in Python.

# storing items 2,3 and 4 in the list
x = [2,3,4]
# using clear() method to remove all items
x.clear()
# print list
print(x)

Output(1)

[]

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *