The List is a collection of heterogeneous data type that is ordered and changeable i.e. mutable. It allows duplicate members.
- Creation of list:
- Using square brackets:
# creation of empty list empty_list = [] print(empty_list) mylist = ["apple","orange","kiwi"] print(mylist)
Output:
[] ['apple', 'orange', 'kiwi']
# creation of empty list empty_list2 = list() print(empty_list2) # creation of list of characters mylist2 = list("apple") print(mylist2)
Output:
[] ['a', 'p', 'p', 'l', 'e']
We can access any element from the list by using its index. Indexing in lists starts from 0.
mylist = ["apple","orange","kiwi"] # accessing 1st element print(mylist[0]) # accessing 3rd element print(mylist[2])
Output:
apple kiwi
We can specify a range of indexes by indicating where to begin and where to end the range. When indicating a range, the return value will be a new list with the specified elements.
mylist = ["apple","orange","kiwi", "banana","mango"] # print element from 2nd # index to 5th index print(mylist[2:5])
Output:
['kiwi', 'banana', 'mango']
- Using append() method:
Elements are adding to a list from last position.
mylist = ["apple","orange","kiwi"] # append element in the # list from back side mylist.append("banana") print(mylist)
Output:
['apple', 'orange', 'kiwi', 'banana']
The append() method adds only a single item at the end of the list. Whereas the insert() method is used to add an element at the desired position. This method needs two arguments i.e. (position, value).
mylist = ["apple","orange","kiwi"] # insert element in the list # at 0th index mylist.insert(0,"mango") print(mylist)
Output:
['mango', 'apple', 'orange', 'kiwi']
- Using remove() method:
This method is used to remove an element from the list. You need to mention the element to be removed from the list. In case element is not exist in the list, this method gives error.
mylist = ["apple","orange","kiwi", "banana"] # remove "kiwi" element # from the given list mylist.remove("kiwi") print(mylist)
Output:
['apple', 'orange', 'banana']
This method is by default removes the last element from the list. To remove a specific element from the list, specify its index as an argument to this method. if index is not in the range then it gives error.
mylist =["apple","orange","kiwi", "banana"] # by default pop() method # remove last element # from the list mylist.pop() print(mylist) mylist2 = ["apple","orange","kiwi","banana"] # remove 2nd index element # from the list mylist2.pop(2) print(mylist2)
Output:
['apple', 'orange', 'kiwi'] ['apple', 'orange', 'banana']
Negative indexing refers to the beginning from the end, -1 means last element, -2 means second last element etc.
mylist = ["apple","orange","kiwi"] # accessing last element # form the list print(mylist[-1]) # accessing 3rd last element # form the list print(mylist[-3])
Output:
kiwi apple
To change the value of an element, index number is used.
mylist =["apple","orange","kiwi"] # overwrite 1st index element mylist[1] = "cherry" print(mylist)
Output:
['apple', 'cherry', 'kiwi']
We can iterate through the list using for loop.
mylist =["apple","orange","kiwi"] print("1st Way of for looping:", end=" ") # for loop for element in mylist : # accessing element print(element, end = " ") print("\n2nd Way of for looping:", end = " ") # for loop for i in range (0,3) : # access element through # indexing print(mylist[i], end = " ")
Output:
1st Way of for looping: apple orange kiwi 2nd Way of for looping: apple orange kiwi
To calculate the length of the list len() built – in function is used.
mylist = ["apple","orange","kiwi", "banana","mango"] # length of the given list print(len(mylist))
Output:
5
- Using ‘+’ operator:
We can use the ‘+’ operator to join two or more lists.
list1 = ["apple","banana"] list2 = ["cherry","orange"] # merging two lists mylist = list1 + list2 print(mylist)
Output:
['apple', 'banana', 'cherry', 'orange']
- Using extend() method:
The extend() method is used to add multiple elements to the list simultaneously.
list1 = ["apple","banana"] list2 = ["cherry","orange"] # mergin two lists list1.extend(list2) print(list1)
Output:
['apple', 'banana', 'cherry', 'orange']
- Sorting:
- In Ascending Order:
To sort a list, We are using sort() method of the list.
list1 = [1, 2, 0, 6, 5] list1.sort() print(list1)
Output:
[0, 1, 2, 5, 6]
list1 = [1, 2, 0, 6, 5] list1.sort(reverse = True) print(list1)
Output:
[6, 5, 2, 1, 0]