Yoda

Yoda

Lists in Python

Lists can be used to store multiple elements of the same or different datatypes. Lists are variable length data structure. If you are new you will be using this a lot and if you have been using it, let me share some more tips and tricks.

1
abc = list()

abc.append() Adds at the end of the list. Time Complexity O(1)

1
2
3
4
5
abc.append('a')
print abc
['a']
abc.append('b')
['a', 'b']

Similar methods for adding to a list are .insert(index, elem) & .extend(another_list)

.insert(index, elem) inserts the element at the given index, shifting elements to the right. Time Complexity O(n).

abc.extend(def) adds the elements in list def to the end of the list abc. Time Complexity O(k), where k is the length of list def

1
2
3
4
5
6
7
8
9
def = list()
def.append('d')
print def
['d']
def.append('e')
['d', 'e']
abc.extend(def)
print abc
['a', 'b', 'd', 'e']