List Manipulation in Python Class 11 Notes

What is List in Python?

Python Lists

Like strings, lists are a sequence of values. A list is a data type that can be used to store any type and number of variables and information. The values in the list are called elements or items or list members.

 

A list in Python is formed by enclosing the values inside square brackets ([]). Unlike strings, lists are mutable, i.e., values in a list can be changed or modified and can be accessed using index value enclosed in square brackets.

 

Lists are great, but not suitable for every task. There are other data types out there like arrays, tuples and dictionaries which may be more suitable for certain tasks. First scope out your problem and then decided which data type fits the best.

 

Python Lists Syntax

 

mylist = ["C","C++","Java","Python","PHP"]

 

The items within the list must be enclosed within square brackets, and each item must be separated by a comma. Unlike arrays, Lists can store any type of data type as well as store variables of different datatypes at the same time.

The double quotes are not part of the syntax of the list. They are only necessary if the item is a string.

 

List Indexing

You can access the contents of a list by using it’s index. A list is an ordered collection, so the values can be called in the order in which they were placed inside.

 

Don’t forget that indexing starts from zero in python. The code below will output “C++”.

mylist = ["C","C++","Java","Python","PHP"]
print(mylist[1])

Output:

C++

 

Using negative integers will reverse the order of the list. Negative indexing starts from –

mylist = ["C","C++","Java","Python","PHP"]
print(mylist[-2])

Output:

Python

 

List Manipulation in Python

Selecting a range of items

If you wish to pick up more than just one item in a python list, you can create “slices”. You can specify a range, by giving the starting and ending number. Everything in between will be placed into the slice.

Note that the ending number will not be included in this slice.

mylist = ["C","C++","Java","Python","PHP"]
print(mylist[2:5])

Output:

['Java', 'Python', 'PHP']

 

You don’t nessacerily how to define the starting point, as the default will be 0, or the start of the list. The semi-colon and ending point must be present though.

mylist = ["C","C++","Java","Python","PHP"]
print(mylist[:3])

Output:

['C', 'C++', 'Java']

 

It also works the other way around, where you define the start point, and leave out the end point, while keeping the semi-colon.

mylist = ["C","C++","Java","Python","PHP"]
print(mylist[2:])

Output:

['Java', 'Python', 'PHP']

 

Updating an Item in a List

Updating an item value in a list is fairly straight forward. Refer to the index where you want to change the value and then assign a value to it.

mylist = ["C","C++","Java","Python","PHP"]
mylist[2]="Rust"
print(mylist)

Output:

['C', 'C++', 'Rust', 'Python', 'PHP']

 

Adding an Item to a List

 

The append() adds an item to the end of the list.

mylist = ["C","C++","Java","Python","PHP"]
mylist.append("Rust")
print(mylist)

Output:

['C', 'C++', 'Java', 'Python', 'PHP', 'Rust']

 

The insert() function takes two parameters. The first is the position you want to add a value, and the second parameter is the value itself.

mylist = ["C","C++","Java","Python","PHP"]
mylist.insert(2,"Rust")
print(mylist)

 

Output:

['C', 'C++', 'Rust', 'Java', 'Python', 'PHP']

Note that the former second position item, “java” was moved forward to the third position.

 

Removing an Item from a List

The remove() function removes a specific item from a list.

 

mylist = ["C","C++","Java","Python","PHP"]
mylist.remove("C++")
print(mylist)

Output:

['C', 'Java', 'Python', 'PHP']

Unlike remove(), the pop() function takes an index as a parameter, and removes the item at that index. However, the pop function is also often used without any parameters, in which case, it will remove the last item.

mylist = ["C","C++","Java","Python","PHP"]
mylist.pop(0)
print(mylist)

Output:

['C++', 'Java', 'Python', 'PHP']

 

Pop() function without any parameters.

mylist = ["C","C++","Java","Python","PHP"]
mylist.pop()
print(mylist)

Output:

['C', 'C++', 'Java', 'Python']

 

The del() function has two functions. Like, the pop() function it can delete an item at a specific index, as well as delete a whole list in one go.

mylist = ["C","C++","Java","Python","PHP"]
del mylist

 

You can delete the value at a specific index using the del keyword in the following manner.

mylist = ["C","C++","Java","Python","PHP"]
del mylist[2]
print(mylist)

Output:

['C', 'C++', 'Python', 'PHP']

 

Other Useful Operations

 

To check for a specific item in a list

mylist = ["C","C++","Java","Python","PHP"]
if "C" in mylist:
    print("Yes")

Output:

Yes

 

Finding the length of a list

mylist = ["C","C++","Java","Python","PHP"]
length = len(mylist)
print(length)

 

Output:

5

 

Iterating through a list.

mylist = ["C","C++","Java","Python","PHP"]
for x in mylist:
    print(x)

Output:

C
C++
Java
Python
PHP

Sorting a list

Using the sort function you can sort a list in ascending order. Works on both alphabets and numbers. Be careful as this function alters the list itself.

mylist = [1,3,2,6,7,9,8,15,11]
mylist.sort()
print(mylist)

Output:

[1, 2, 3, 6, 7, 8, 9, 11, 15]

 

Concatenating two lists:

mylist1 = [1,3,2,6]
mylist2 = ["apple", "banana"]
mylist=mylist1+mylist2
print(mylist)

Output:

[1, 3, 2, 6, 'apple', 'banana']
Copywrite © 2020-2024, CBSE Python,
All Rights Reserved
error: Content is protected !!