Python Lists: Modifying, Adding, Removing Objects & Quick Practice Programs

Lists Can Be Modified

Python lists are mutable.

Mutable means that we can change the objects stored in a list after the list has been created.

For example:

numbers = [10, 20, 30, 40, 50]
numbers[2] = 100
print(numbers)

Output:

[10, 20, 100, 40, 50]

Originally:

[ 10, 20, 30, 40, 50 ]

After changing the object at index 2:

[ 10, 20, 100, 40, 50 ]

The third object has been changed from 30 to 100.

Adding an Object to a List

We can add a new object to the end of a list using append().

Example:

numbers = [10, 20, 30]
numbers.append(40)
print(numbers)

Output:

[10, 20, 30, 40]

Before append():

[10, 20, 30]

After:

[10, 20, 30, 40]

The object 40 has been added to the end.

Removing an Object from a List

We can remove an object using remove().

numbers = [10, 20, 30, 40, 50]
numbers.remove(30)
print(numbers)

Output:

[10, 20, 40, 50]

The object 30 has been removed.

Changing Multiple Objects

Because lists are mutable, we can change their objects.

marks = [50, 60, 70, 80, 90]
marks[0] = 55
marks[4] = 95
print(marks)

Output:

[55, 60, 70, 80, 95]

An Empty List

A list does not have to contain objects when it is created.

numbers = [ ]
print(numbers)

Output:

[ ]

We can later add objects:

numbers = [ ]
numbers.append(10)
numbers.append(20)
numbers.append(30)
print(numbers)

Output:

[10, 20, 30]

Important Properties of Python Lists

We can summarize the basic properties of lists as follows:

1. Lists store multiple objects

numbers = [ 10, 20, 30, 40, 50 ]

A list can store multiple objects in a single variable.

2. Lists are ordered

The objects have a specific order.

numbers = [ 10, 20, 30 ]

The order of objects is maintained in the list.

3. Lists use square brackets

A list is created using square brackets [ ].

[ 10, 20, 30 ]

The opening and closing [ ] identify a list.

4. Lists allow duplicate objects

A list can contain the same object multiple times.

For example:

numbers = [ 10, 20, 10, 30, 10 ]
print( numbers)

Output:

[ 10, 20, 10, 30, 10 ]

The value 10 can appear multiple times.

5. Lists can contain different types of objects

A list can contain objects of different data types.

data = [ 10, 3.14, “Hello”, True ]

Here, the list contains an integer, float, string, and Boolean value.

6. Lists are mutable

Objects in a list can be changed, added, or removed after the list is created.

numbers = [ 10, 20, 30 ]
numbers[1] = 50

The value at index 1 can be changed from 20 to 50.

7. Lists use zero-based indexing

The first object in a list has index 0.

numbers = [ 10, 20, 30 ]
print( numbers[0])
Output: 10

Therefore, the first object has index 0, the second has index 1, and so on.

A Simple Real-World Example

Suppose we want to store the marks of five students:

marks = [ 85, 72, 91, 68, 77 ]
print( marks)
print( marks[0])
print( marks[2])
print( len( marks))

Output:

[ 85, 72, 91, 68, 77 ]
85
91
5

Quick Practice Programs

Try running these programs yourself:

Practice 1

numbers = [ 5, 10, 15, 20, 25 ]
print( numbers)

Practice 2

names = [ “Amit”, “Ravi”, “John”, “Ali”, “David” ]
print( names[0])
print( names[4])

Practice 3

data = [ 100, 25.5, “Python”, True, 200 ]
print( data)
print( len( data))

Practice 4

numbers = [ 10, 20, 30, 40, 50 ]
numbers[ 1] = 200
print( numbers)

Practice 5

numbers = [ 10, 20, 30 ]
numbers.append( 40)
print( numbers)

Practice 6

numbers = [ 10, 20, 30, 40 ]
numbers.remove( 20)
print( numbers)

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

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

Translate »