1. LIST:
1.1 introduction to list :
- A list is a collection which is ordered and changeable. In Python lists are written with square brackets.
- list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. They enable you to keep data together that belongs together, condense your code, and perform the same methods and operations on multiple values at once.
Examples :
- Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
- Print the single item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1]) #output banana
-
Changing the item of the list:
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant" #output ['apple', 'blackcurrant', 'cherry']
print(thislist)
1.2 Loop Through a List :
- we can loop through the list items by using a FOR loop
Examples :
- Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"]
for x in thislist: #output apple
print(x) banana
cherry
1.3 Check the Item Exists in the list :
- To determine if a specified item is present in a list use the IN keyword.
Examples
- Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist: #output Yes, 'apple' is in the fruits list
print("Yes, 'apple' is in the fruits list")
1.4 ADD and REMOVE the item in the list :
- To add an item to the end of the list, use the APPEND() method.
- REMOVE() or POP() or DEL is used to remove the item in the list.
Example :
- Add orange to the existing list :
thislist = ["apple", "banana", "cherry"]
thislist.append("orange") #output ['apple', 'banana', 'cherry', 'orange']
print(thislist)
- removing the item using remove() method :
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana") #output ['apple', 'cherry']
print(thislist)
-
removing the item using pop() method :
thislist = ["apple", "banana", "cherry"]
thislist.pop() #output ['apple', 'banana']
print(thislist)
-
removing the item using del keyword :
thislist = ["apple", "banana", "cherry"]
del thislist[0] #output ['banana', 'cherry']
print(thislist)
1.5 The list() Constructor :
- It is also possible to use the list() constructor to make a list.
- Using the list() constructor to make a List.
Example :
- constructor in list.
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets
print(thislist)
1.6 List Methods :
- Following table shows the diffrent methods in list.
| Method |
Description |
| append() |
Adds an element at the end of the list |
| clear() |
Removes all the elements from the list |
| copy() |
Removes all the elements from the list |
| count() |
Returns the number of elements with the specified value |
| extend() |
Add the elements of a list (or any iterable), to the end of the current list |
| index() |
Returns the index of the first element with the specified value |
| insert() |
Adds an element at the specified position |
| pop() |
Removes the element at the specified position |
| remove() |
Removes the item with the specified value |
| reverse() |
Reverses the order of the list |
| sort() |
Sorts the list |