List Method Part 2

In this blog, we will be discussing about some of the list methods. List methods can be used for inserting item into a list, appending a new item into a list, sorting items in a list and many more

Append Method

Append new item or adding new item at the end of the list. Syntax listname.append(item)

In the below code, we have created a list called num. Displaying the items inside original list then adding new item to a list by using append method and display it.

num = [1,2,3]
print("Original List")
print(num)
num.append(4)
print("Appending new item")
print(num)

Output

Original List
[1, 2, 3]
Appending new item
[1, 2, 3, 4]

Index method

This method returns index position for a value. Syntax listname.index(index_number)

Example : Below list include city names. Using index number with index method to display its associated value.

city = ['Paris','London','New York','Sydney','Delhi']
print(city.index('London'))

Output

1
Index NumberValue
0Paris
1London
2New York
3Sydney
4Delhi

Insert Method

Insert method is use to insert new item into a list. We need to specify index number to insert new item at that index number. Syntax listname.insert(index_number, value)

In the below example, city list is created with 5 values. Inserting a new item at index position 3.

city = ['Paris','London','New York','Sydney','Delhi']
print('Original list')
print(city)
city.insert(3,'Tokyo')
print("Inserting new item at index number 3")
print(city)

Output

Original List
['Paris', 'London', 'New York', 'Sydney', 'Delhi']
Inserting new item at index number 3
['Paris', 'London', 'New York', 'Tokyo', 'Sydney', 'Delhi']

Extend Method

Extend method can be use to append several items at once. Syntax list1.extend(list2)

num1 = [1,2,3,4]
num2 = [5,6,7,8]
print('Before extending num1 list')
print(num1)
num1.extend(num2)
print('After extending num1 list')
print(num1)

Output

Before extending num1 list
[1, 2, 3, 4]
After extending num1 list
[1, 2, 3, 4, 5, 6, 7, 8]

Sort method

Sort method is use for sorting items in a list. Syntax listname.sort(). Sort method returns none value. If we try to print output of sort method, it will return none value. Below is the example

num = [4,3,56,7,2,8,1,9,10.44,3]
print(num.sort())

Output

None

Also if we try to assign output of sort method to another list, it will return none output.

num = [4,3,56,7,2,8,1,9,10.44,3]
num1 = num.sort()
print(num1)

Output

None

Now we will be creating a list which include numbers arrange in an unsorted manner. Then applying sort method on the list and printing items inside the list.

num = [4,3,56,7,2,8,1,9,10.44,3]
print('Original place of items inside a list')
print(num)
num.sort()
print("Items sorted inside a list")
print(num)

Output

Original place of items inside a list
[4, 3, 56, 7, 2, 8, 1, 9, 10.44, 3]
Items sorted inside a list
[1, 2, 3, 3, 4, 7, 8, 9, 10.44, 56]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s