Quick Reach
The List in Python
The List is among the compound datatypes available in Python. Other compound data types like Tuple and Dict are explained in their respective chapters. Among the other compound data types, the List is the most versatile.
An example of List
The List literals are written within the brackets separated by commas. Python has built-in list functions that can be used to sort, append, remove, finding the length of list etc.
These functions are covered in separate chapters and links are given at the bottom of this tutorial.
Main points about List
- Python List items (values) can be written as comma separated values in square [] brackets.
For example: list_name = []
- Lists in Python can store different types of values or items like numbers and strings.
e.g. Py_list = [1, 2, ‘Python’, ‘lists’ ]
- The list data type implements sequence protocol and allows to add or remove items from the list.
A few examples of lists
list_number = [1,2,3,4,5]
list_strings = [‘this’, ‘is’, ‘List’, ‘lesson’]
list_mixed = [1, ‘this’, 2, ‘is’, 3, ‘list’, 4, ‘chapter’]
Creating and accessing list example
In the following example, a List of numbers is created. After that, we used a for loop to display the list items by using the print function.
1
2
3
4
5
6
7
|
# This is list example with numbers
list_number = [1,2,3,4,5]
for i in list_number:
print (i)
|
The output:
1
2
3
4
5
An example of list of strings
Following example creates a Python List of strings with four elements. After creating the list, the for loop is used to display list elements. Note that, the for loop is using the List as sequence and assigns it to the variable i.
1
2
3
4
5
6
7
|
# This is list example with strings
list_strings = [‘this’, ‘is’, ‘list’, ‘lesson’]
for i in list_strings:
print (i)
|
The Output:
this
is
list
lesson
Accessing specific item in list python example
In the following example, a List with mixed items is created. The list contains numbers and strings. The print function is used to display specific list elements by using the index number.
1
2
3
4
5
|
# This is list example with mixed items
list_mixed = [1, ‘this’, 2, ‘is ‘, 3, ‘Python list’, 4, ‘chapter’]
print (list_mixed[1], list_mixed[3], list_mixed[5], list_mixed[7])
|
The Output of above example:
this is python list chapter
List built-in methods
Python also provides useful methods of the List type that makes programmers life quite easy. A few methods are listed below with a brief along with the links to their respective chapter.
List length method
The length method of the List is used to return the total number of elements in the list. The function name is len().
An example of Python length method
1
2
3
|
list_len = [‘a’,‘b’,‘c’]
print (“List length is = “, len(list_len))
|
Go to List length method chapter.
List Remove method
The List remove method is used to remove elements from an existing list.
An example of Python List remove method is given below. See it by clicking the link below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# This is remove method example
list_Py = [1, ‘this’, 2, ‘is’, 3, ‘list’, 4, ‘chapter’]
print (list_Py)
list_Py.remove(1)
list_Py.remove(2)
list_Py.remove(3)
list_Py.remove(4)
print (list_Py)
|
Go to the list remove method chapter for full detail and examples.
The list Append method
The list append method is used to add an element to an existing list.
An example of the Append method
1
2
3
4
5
6
7
8
9
|
# This is append method example
list_Py = [‘this’,‘is’,‘list’,‘chapter’]
print (“List before python append:”,list_Py)
list_Py.append (‘explaning append method’)
print (“List after python append:”,list_Py)
|
Go to Append to list chapter for full detail
List sort method
The sort method is used to sort the list in ASC or DESC order.
For full detail go to Sort list chapter.
Leave A Comment?