A list is a collection of elements. All the elements of a list need not be of a single type, they can also be of multiple types. This means that a single list can contain a string, an integer, a float etc.
Elements of a list are also called list items. A list is created by enclosing elements between square brackets([ and ]). Example,

list = [“codippa”, 2, 5.6]
print(list)      # prints [“codippa”, 2, 5.6]

Printing a list
A list can be directly printed using print function. Just supply the list variable as argument to the function and it will print the contents of the list.
Creating an empty list
Many times you do not want to assign elements to a list at the time it is created since it might happen that the elements are being added to the list dynamically. It is possible to create empty lists and elements to them later. There are two ways of creating an empty list

    1. Using Empty square brackets
Create a variable and assign it empty square brackets as shown below.

mylist = []       # create empty list
type(mylist)      # prints <class 'list'>
mylist.append(1)  # add an element to list
print(mylist)     # prints [1]

    2. Using List constructor
Create a variable and assign it an object of class list. For creating an object of class list, write list followed by parenthesis as  shown below. This syntax is called the constructor of inbuilt  listclass.

mylist = list()   # create object of list
type(mylist)      # prints <class 'list'>
mylist.append(1)  # add an element to list
print(mylist)     # prints [1]

Note: If you get an error as TypeError: ‘list’ object is not callable while using second method, then you might have used a variable with name list somewhere in your code before using this syntax and you have overridden python’s built-in list with your variable. To resolve the error remove your variable from current session by using del <variable_name>

Accessing elements of a list
Elements of a list can be accessed using an index where index of first element is 0, second is 1 and so on. Thus, to access 3rd element of a list, use

list = [“codippa”, 2, 5.6]
print(list[2])     # prints 5.6

Modifying value of list item
It is also possible to update an item of a list after list creation. Simply assign a value to the index which you want to update. Example, suppose a list is

list = [“codippa”, 2, 5.6]
list[0] = "the website"   # assign a new value to first item
print(list)               # prints [“the website”, 2, 5.6]

It is not necessary that the new value should be of same type as the existing value. Thus,

list[0] = 25 is also valid.

Checking the type of a list
If you want to check if a variable is of type list, then at the python prompt, use type function with the variable as argument as

list = [“codippa”, 2, 5.6]
type(list)          # prints <class ‘list’>

Appending to a list
It is also possible to add new items to the end of an existing list using append function. This function takes a single argument which is the value of item to be appended.

list = [“codippa”, 2, 5.6]
list.append("python")
print(list)        # prints [“codippa”, 2, 5.6, "tutorial"]

It is also possible to add new items  in between a list using insert method. It takes two arguments: first is the index at which the new item will be added and second is the value of new item. Index is zero based.

list = [“codippa”, 2, 5.6]
list.insert(1, 'New!!')   # adds item to second position
print(list)               # prints [“codippa”, "New!!", 2, 5.6]

If you give an index which is greater than the size of the list, then the new  item will be added at the last position.
Removing an item from list
It is possible to remove an item from a list using various methods. Detailed explanation of all the methods is available here.
Iterating over a list
In order to iterate or loop a list, use the for loop with the in keyword. Example,

list = [“codippa”, 2, 5.6]
for item in list:
   print(item)   # print each item

Here item is a user defined variable which contains the value of a list element in every iteration.
Above code produces the following output

codippa
2
5.6

For more details on how to use loops in python, head over to this section.
Combine two lists
It is possible to directly combine two lists in python. This means that elements of both the lists are merged together. For combining two lists in python, use the concatenation or plus operator between the lists to be combined.
The result is a list that contains the elements of both the lists. Example,

list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_sum = list_one + list_two
print(list_sum)

This prints

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

Remember that this method simply merges the elements of both the lists into a third list. It does not remove any duplicate elements.
Thus, if both the lists contain the same element, then it will be present in the resulting list twice.

Leave a Reply

Mark Your Impression

  Subscribe  
Notify of
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.