Python List | Methods in Python List | Accessing Python List| Updating Values | Looping with Code Samples

List is used to store sequence of values of different types. The values or data in list are called Elements or Items. These items in a list are enclosed within square bracket [] and separated by using commas (,).

For example,

L1 = [1, 2, 3, 4, 5, 6] //List with integer values

L2 = [“Apple”, 10, 3.2] // List with various types

print(L1)

print(L2)

Output

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

[‘Apple’, 10, 3.2]

Python list are ordered and modifiable. It can be able to change the elements after creating a list.

Methods used in Python List

Python Methods
  1. append()

This method is used to add an element to the end of the list.

L2 = [“Apple”, “Orange”, 3]

L2.append(“Grapes”) //Adding item

print(L2)

Output

[‘Apple’, ‘Orange’, 3,’Grapes’]

2. extend()

This method is used to add all elements of a list to another list.

L2 = [“Apple”, “Orange”, 3, “Grapes”]

L3 = [1,2]

L2.extend (L3) //Adding a list to another list

print(L2)

Output

[‘Apple’, ‘Orange’, 3, ’Grapes’, 1 , 2]

3. sort()

This method is used to sort items in a list in ascending order.

L4 = [5,7,6,4,8,2]

L4.sort() //Sorting in ascending order

print(L4)

Output

[2 , 4 , 5 , 6 , 7 , 8]

5. reverse()

This method is used to make the items of a list in reverse order.

L4 = [5,7,6,4,8,2]

L4.reverse() //Making items in reverse order

print(L4)

Output

[2, 8 , 4 , 6 , 7 , 5]

6. pop()

This method is used to remove an item from the end of a list or from a specified index.

L4 = [5,7,6,4,8,2]

L4.pop() //Removing an item

print(L4)

Output

[5, 7, 6, 4, 8]

7. remove()

This method is used to remove an item from the list.

L4 = [5,7,6,4,8,2]

L4.remove(6) //To remove a specific item

print(L4)

Output

[5, 7, 4, 8, 2]

8. insert()

This method is used to add an item in the specified index.

L4 = [5,7,4,8,2]

L4.insert(1, ”apple”) //To add a new item “apple” at index 1

print(L4)

Output

[5, ‘apple’, 7, 4, 8, 2]

9. len()

This method is used to count the number of items in a list.

L4 = [5,7,4,8,2]

print(len(L4)) //To find length of list

Output

5

How to access item from a Python List

In python, list items are accessed by using index number. The index of an item is starting from 0. Suppose, a list of n elements has an index from 0 to (n-1). Index of a list item is represented by using the index operator [].

Example,

0, 1, 2, 3, and 4 are the index numbers of items p, q, r, s and t respectively.

New = [‘p’, ’q’, ’r’, ’s’, ’t’]

//To access items from the start

print(New[0])

print(New[1])

print(New[2])

Output

P

q

r

Negative Indexing

In Negative indexing, accessing of items are beginning from the end of the list. -1 refers to the last item, -2 refers to the second last item and so on.

New = [‘p’, ’q’, ’r’, ’s’, ’t’]

//To access items from the end

print(New[-1])

print(New[-2])

print(New[-3])

Output

t

s

r

Range of Index

Range of index can be accessed by using slice operator ‘ : ‘ which is enclosed by the index operator [ ]. It can be access items from a list within a specified range.

Syntax : Listname[starting index : ending index]

Example,

New = [‘p’, ’q’, ’r’, ’s’, ’t’]

//To access items within specified range

print(New[0:]) //To access items from index 0

print(New[1:3]) //To access items from index 1 to 3

print(New[0:2:4]) //To access items from index 0, 2 and 4

print(New[:-2]) //To access items from index -2

Output

[‘p’, ’q’, ’r’, ’s’, ’t’]

[’q’, ’r’, ’s’]

[‘p’, ’r’, ’t’]

[‘p’, ’q’, ’r’]

Updating Values in List

List is a mutable data structure in python. Mutable means changeable. The items of a list can be updated by using the slice and assignment operator.

Example,

New = [‘p’, ’q’, ’r’, ’s’, ’t’]

print(New)

New[2] = ‘m’ //To update value to the second index

print(New)

New[1:3] = [1, 2] //Adding multiple items

print(New)

New[-1] = 33 // To add value at the end of the list

print(New)

Output:

[‘p’, ’q’, ’r’, ’s’, ’t’]

[‘p’, ’q’, ’m’, ’s’, ’t’]

[‘p’, 1, ’r’, 2, ’t’]

[‘p’, 1, ’r’, 2, 33]

Looping through list

For loop

The for loop is used to execute a set of statements by iterate over the items of the list and contains each item in each iteration.

Syntax : for i in list name: print(i)

The value of i is starting from 0.

Example,

sample = [1,2,3,4]

for i in list:

print(i) // The i variable will iterate over the items of the list and contains each item in each iteration

Output

1

2

3

4

Python Developer at TimeClick Software