Creating and Modifying Lists in Python

List stores pieces of information in a sequence within a variable.

Creating – We create a list and store it under a variable called my_list. In python, to create a list, we need to mention the items within square brackets “[]”. Here we create a list of string items, hence each item is written within double quotes.

my_list = ["frog", "dino", "lion", "rabbit"]
my_list

[‘frog’, ‘dino’, ‘lion’, ‘rabbit’]

Appending – We use append to add an item at the end of the list. Here, we append “tiger” to the list.

my_list.append("tiger")
my_list

[‘frog’, ‘dino’, ‘lion’, ‘rabbit’, ‘tiger’]

Creating – Now we create another list called new_list.

new_list = ["red","blue","orange"]

Extending – We use extend to add one list to another list. Here we extend new_list by my_list.

new_list.extend(my_list) 
new_list

[‘red’, ‘blue’, ‘orange’, ‘frog’, ‘dino’, ‘lion’, ‘rabbit’, ‘tiger’]

Insertion – We use insert to add a new item at a specified position in a list. Here, we insert a new item “snake” at position 2 of my_list. Index number in python lists starts from 0. Here, “frog” is at 0 index number, “dino” at 1 index number and so on.

my_list.insert(2,"snake")
my_list

[‘frog’, ‘dino’, ‘snake’, ‘lion’, ‘rabbit’, ‘tiger’]

Removing by item name – Remove looks for the first mention of the specified item name in the list and deletes it. Here, remove looks for first mention of “dino” in my_list and removes it.

my_list.remove("dino")
my_list

[‘frog’, ‘snake’, ‘lion’, ‘rabbit’, ‘tiger’]

Removing by index number – Pop removes the item at the specified index number. Here, item mentioned at the index number 3 i.e. “rabbit” is removed.

my_list.pop(3)
my_list

[‘frog’, ‘snake’, ‘lion’, ‘tiger’]

Finding index number – Index finds the index number of the first mention of specified item. Here, we find index number for item “lion” in my_list.

my_list.index("lion")

2

Counting item mentions – Count gives the number of mentions of the specified item in the list. Here, we get the count of item “snake” in my_list.

my_list.count("snake")

1

Sorting(Ascending) – Sort helps to sort list in ascending or descending order. By default it sorts the list in ascending order. Here, we sort my_list in ascending order.

my_list.sort()
my_list

[‘frog’, ‘lion’, ‘snake’, ‘tiger’]

Sorting(Descending) – To sort the list in descending order, we set reverse option as TRUE. Here, we sort my_list in descending order.

my_list.sort(reverse=True)
my_list

[‘tiger’, ‘snake’, ‘lion’, ‘frog’]

Reversing – We use reverse to reverse the items of a list such that the item at position 0 takes position len(list), item at position 1 takes position len(list-1) and so on. Here, we reverse new_list

new_list.reverse()
new_list

[‘tiger’, ‘rabbit’, ‘lion’, ‘dino’, ‘frog’, ‘orange’, ‘blue’, ‘red’]

Next, you may like to read about How to use lists as Stacks and Queues.

Advertisements

One thought on “Creating and Modifying Lists in Python

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s