List is a data structure similar to array. The List in Python can hold any number of elements in it. It does not have a fixed size.
List is indexed. It means we can access the elements of a list by mentioning its index number. Indexing starts from 0, and it starts from the left side of the list as shown below. This is a list of apples. Here we have 6 apples in the list. Hence the size of the list is 6. But since indexing starts from 0, the index numbers of list are 0,1,2,3,4,5.
List is denoted by square brackets [] in Python. The syntax is as below:
Syntax:
listName = [element1, element2, ......, elementN]
Examples:
colors = ["red", "blue", "pink", "green"]
numbers = [2,12,10,25,6,3]
How to create a list?
List can be created by keeping all elements inside a square bracket and assigning it to a list name. List can also be created using list() constructor. When using list() constructor, all the elements must be mentioned within double round brackets.
Examples:
colors = ["Red", "Black"]
nums = list((3,2,5,6,71,57,22,29))
List can contain different types of elements:
List can contain elements of different data types in it. It can have int, string, float, and other types in it. Let us see an example list named as 'myList'.
myList = [20,"Paris","Noodles",2.13,0,1,""Apple"]
List can contain another list or iterable:
List can also contain another list within it. Similarly it can contain tuple, set, dictionary also within it. Let us see an example list below:
nums = [2,3,4,1,45]
fruitsTuple = ("Orange", "Apple", "Plum", "Peach")
myList = ["computer", 2, nums, fruitsTuple, 4]
In the above example, the list myList has list and tuple within it at indices 2 and 3. A list can contain any number of lists or other iterables such as tuple, set, dict.
Accessing Elements of List:
To access list elements, we use indexing. Consider N as the size of list, indexing starts from 0 till N-1. This indexing starts from the left side of the list always.
Example:
nums = [52,4,1,40,2]
print(nums[0])
print(nums[1])
print(nums[2])
print(nums[3])
Explanation: In the above lines of code, on printing nums[0], the element at index 0 will be printed. The element at index 0 is 52. The element at nums[1] is 4, element at nums[2] is 1, element at nums[3] is 40, element at index 4 is mentioned as nums[4]. It is 2.
Negative Indexing of List:
List can also be accessed by negative indexing. Negative indexing starts from -1. Negative indexing is taken from the right most side of the list.
Examples:
basket = ["potato", "pineapple", "orange", "spinach"]
print(nums[-1]) # prints "spinach"
print(nums[-2]) # prints "orange"
print(nums[-3]) # prints "pineapple"
print(nums[-4]) # prints "potato"
In the above line of code, basket is a list. By negative indexing, the element at index -1 is spinach, as it is at the right most side of list. The element at index -2 is orange.
Length of list:
To find the number of elements in a list, len() method is used.
Example:
nums = [2,4,5,"Hello", "Cake"]
print(len(nums))
Now the length of list is printed as 5, because there are 5 elements in it.
Type of list:
The type of list can be found using type() method.
Example:
nums = [3,5,1]
print(type(nums))
The output will be as below:
<class 'list'>
List can contain duplicate elements:
List can contain any same element for any number of times. List allows duplicate elements in it. In the example below, list has "red" three times in it, "blue" is appearing two times.
colorsList = ["red", "blue" ,"red", "yellow", "black", "red", "blue"]
Empty list:
List can be created with no elements in it. Simply assign square brackets to a list name.
myList = [] # This is an empty list. Length of this list is 0
Now we have learned about the basics of List in Python. In the next article, we will be knowing about the operations on list.
Hope my article is useful for you in knowing about List in python.
Thanks for reading!