Python Lists
A list in python is a compound data type which can store multiple elements.
- Items of a list are enclosed within square brackets ([])
- Items are separated by commas
- The items of a list can be of different data type.
Ex: l1 = [1,2,”a”,2.3,3j] - Slice operator ([ ] and [:] ) can be used to access elements in a list. Negative index: count from the right. Ex: print(l1[-2]) returns 2.3
- Indexes start at 0
- Concatenation operator: plus (+) sign. Ex: [1, 2, 3] + [4, 5, 6]returns [1, 2, 3, 4, 5, 6]
- Repetition operator: asterisk (*). Ex: [‘Hi!’] * 4 returns [‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’]
- len(list) returns the number of elements in the list. Ex: len([1, 2, 3]) returns 3
- in operator returns the membership of an element in a list. Ex: 3 in [1, 2, 3] returns True
l1 = [1,2,"a",2.3,3j] l2 = [567, "jonny"] print(l1) # Prints all the elements of list print(l1[0]) # Prints 1st elements of the list print(l1[1:3]) # Prints elements from 2nd till 3rd print(l1[2:]) # Prints all the elements of l1 from 3rd value print(l2 * 2) # Prints list l2 twice print(l1 + l2) # Prints concatenated lists l1[2]=3 # Updates 3rd element of the list print(l1[2]) # Prints updated 3rd element of the list del l1[2] # Deleting 3rd element of the list print(l1)
OUTPUT:
[1, 2, 'a', 2.3, 3j] 1 [2, 'a'] ['a', 2.3, 3j] [567, 'jonny', 567, 'jonny'] [1, 2, 'a', 2.3, 3j, 567, 'jonny'] 3 [1, 2, 2.3, 3j, 567, 'jonny']
Built-in List Functions & Methods:
Functions defined for lists in python:
Methods | Returns | Description/Example |
---|---|---|
cmp(list1, list2) | if (x ((x-y)<0) returns -1 if (x==y) => ((x-y)==0) returns 0 if (x>y) => ((x-y)>0) returns 1 |
Compares list1 with list2 |
len(list) | Number of elements of the list | len([1, 2, 3]) returns 3 |
max(list) | Item with max value | max([1, 2, 3]) returns 3 |
min(list) | Item with min value | min([1, 2, 3]) returns 1 |
append(obj) | It returns nothing. Appends object obj to list | l1=[1,2,3]; l1.append([4,5]) print(l1) prints [1,2,3,4,5] |
count(obj) | Returns count of how many times obj occurs in list | l1=[1,2,3,2]; l1.count(2) returns 2 |
extend(seq) | It returns nothing. Appends the contents of seq to list | l1=[1,2,3]; l1.extend([4,5]); print(l1) prints [1,2,3,4,5] |
index(obj) | Returns the first index where obj is found in list | l1=[1,2,3,2]; l1.index(2) prints 1 |
insert(index, obj) | It returns nothing. Inserts object obj into list at offset index | l1=[1,2,3]; l1.insert(4,1); print(l1) prints [1,4,2,3] |
pop(index) | Removes and return object from index. If no index is passed, removes and returns the last item | l1=[1,2,3,2]; l1.pop(1); print(l1) prints 2 [1,3,2] |
remove(obj) | It returns nothing. Remove the first item from the list whose value is obj. | l1=[1,2,3,2]; l1.remove(2); print(l1) prints [1,3,2] |
reverse() | Returns nothing. Reverses objects of list in place | l1=[1,2,3,2]; l1.reverse(); print(l1) prints [2,3,2,1] |
sort() | Returns nothing. Sorts objects of list | l1=[1,2,3,2]; l1.sort(); print(l1) prints [1,2,2,3] |