Lists - Python
List is a collection which is ordered and changeable. Allows duplicate members.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list=["apple","ball","cat","dog"]
>>> print(list)
['apple', 'ball', 'cat', 'dog']
You access the list items by referring to the index number ( indexing starts from o ) :
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>list=["apple","car","dog","gear"]
>>>print(list[1])
car
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>list=["apple","car","dog","gear"]
>>>print(list[-2])
dog
You can specify a range of indexes by specifying where to start and where to end the range.When specifying a range, the return value will be a new list with the specified items.
Return the third, fourth, and fifth item -
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>list=["apple","banana","melon","apricot","kiwi"]
>>>print(list[2:5])
>>>['melon','apricot','kiwi']
Note: The search will start at index 2 (included) and end at index 5 (not included).
Negative indexes starts the search from end of the list.
This example returns the items from index -4 (included) to index -1 (excluded)
>>>list=["apple","banana","melon","apricot","kiwi"]
>>>print(list[-4:-1])
['banana','melon','apricot']
To change the value of a specific item, refer to the index number.
Change the second item:
>>>list=["apple","banana","melon","apricot","kiwi"]
>>>list[1]="black current"
>>>print(list)
['apple','black current','melon','apricot','kiwi']
You can loop through the list items by using a for loop.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x=["paul",2016,3.6]
>>> for i in x:
print(i)
paul
2016
3.6
To determine if a specified item is present in a list use the in keyword.
Check if "apple" is present in the list:
>>>list=["apple","ball","cat","dog"]
>>>if "apple" in list:
>>>("yes, 'apple' is in list")
"yes, 'apple' is in list"
To determine how many items a list has, use the len() method.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> list=["apple","ball","cat","dog"]
>>> print(len(list))
4
To add an item to the end of the list, use the append() method.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> list=["good","sad","bad","better"]
>>> list.append("best")
>>> print(list)
['good', 'sad', 'bad', 'better', 'best']
To add an item at the specified index, use the insert() method.
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> list=["pen","pencil","eraser","sharpener"]
>>> list.insert(1,"scale")
>>> print(list)
['pen', 'scale', 'pencil', 'eraser', 'sharpener']
To remove items from the list various methdos are used:
1. The remove() method removes the specified item
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list=["apple","ball","cat","dog"]
>>> list.remove("ball")
>>> print(list)
['apple', 'cat', 'dog']
2. The pop() method removes the specified index, (or the last item if index is not specified):
>>>list=["apple","ball","cat","dog"]
>>>list.pop(1)
>>>'ball'
>>>list.pop()
>>>'dog'
3. The del keyword removes the specified index and also deletes the entire list.
>>> list=["mom","dad","son","daughter"]
>>> del list[0]
>>> print(list)
['dad', 'son', 'daughter']
>>>list=["apple","ball","cat","dog"]
>>>delete list
SyntaxError: invalid syntax
print(list) #this will cause an error because you have succsesfully deleted "list".
4. The clear() method empties the list:
>>> list=["plates","glasses","spoons"]
>>> list.clear()
>>> print(list)
[]
>>>
We cannot copy a list simply by typing list2 = list1 because, list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one way is to use the built-in List method copy().
Make a copy of a list with the copy() method.
>>> list1=["apple","banana","cherry","strawberry"]
>>> list2=list1.copy()
>>> print(list2)
['apple', 'banana', 'cherry', 'strawberry']
Another way to make a copy is to use the built-in method list().
>>>thislist = ["apple", "banana", "cherry"]
>>>mylist = list(thislist)
>>>print(mylist)
['apple','banana','cherry']
There are several ways to join, two or more lists in Python.
One of the easiest ways are by using the + operator.
>>> list=["a","b","c"]
>>> list1=[1,2,3]
>>> listnew=list+list1
>>> print(listnew)
['a', 'b', 'c', 1, 2, 3]
Another way to join two lists are by appending all the items from list2 into list1, one by one.
>>> list1=[1,2,3]
>>> list2=["a","b","c"]
>>> for i in list2:
list1.append(i)
print(list1)
[1, 2, 3, 'a']
[1, 2, 3, 'a', 'b']
[1, 2, 3, 'a', 'b', 'c']
We can use the extend() method, which purpose is to add elements from one list to another list.
>>> list2=[2.5,3.5,6.5]
>>> list1=[2,3,4]
>>> list1.extend(list2)
>>> print(list1)
[2, 3, 4, 2.5, 3.5, 6.5]
It is also possible to use the list() constructor to make a new list.
>>>thislist=(("apple","ball"))
>>>print(thislist)
('apple','ball')
S.no. | Function | Description |
---|---|---|
1 | cmp(list1,list2) | compares the elements of both the lists |
2 | len(list) | It is used to calculate length of the list |
3 | max(list) | It returns maximum elements of the list |
4 | min(list) | returns the minimum elements of the list |
5 | list(seq) | converts any sequence to the list |
S.no. | Method | Description |
---|---|---|
1 | append() | Adds an element at the end of the list |
2 | clear() | Removes all the elements from the list |
3 | copy() | Returns a copy of the list |
4 | count() | Returns the number of elements with the specified value |
5 | extend() | Add the elements of a list (or any iterable), to the end of the current list |
6 | index() | Returns the index of the first element with the specified value |
7 | insert() | Adds an element at the specified position |
8 | pop() | Removes the element at the specified position |
9 | remove() | Removes the item with the specified value |
10 | reverse() | Reverses the order of the list |
11 | sort() | Sorts the list |