Lists

Lists are a data structure that are an ordered collection (sequence) of objects. They can hold anything in Python, but good practice says you should use list instances to hold only one data type at a time, e.g. a list of numbers, or a list of strings.

To create a new list, you have two ways: literally or through the list() class constructor.

In [1]:
l = []
In [2]:
l2 = list()
In [3]:
l
Out[3]:
[]
In [4]:
l2
Out[4]:
[]
In [5]:
len(l)
Out[5]:
0
In [6]:
l3 = [1,2,3,4]
In [7]:
l3
Out[7]:
[1, 2, 3, 4]
In [8]:
len(l3)
Out[8]:
4
In [9]:
my_list = [1, "Spam", 4, 3.14]
In [10]:
my_list
Out[10]:
[1, 'Spam', 4, 3.14]

List Access & Assignment

Lists can be indexed or sliced using the same exact rules as used for strings.

In [11]:
nums = [34, 26, 15, 10]
nums[2]
Out[11]:
15
In [13]:
x = nums[2]
print(x)
15
In [14]:
nums[2] = 0
nums[2]
Out[14]:
0
In [15]:
nums
Out[15]:
[34, 26, 0, 10]
In [16]:
s = "Hello Yorld"
In [17]:
s[6] = "W"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-938a3b424e13> in <module>()
----> 1 s[6] = "W"

TypeError: 'str' object does not support item assignment
In [20]:
s2 = s.replace("Y", "W")
In [21]:
print(s, s2)
Hello Yorld Hello World
In [22]:
help(list)
Help on class list in module builtins:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iadd__(self, value, /)
 |      Implement self+=value.
 |  
 |  __imul__(self, value, /)
 |      Implement self*=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.n
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(...)
 |      L.__reversed__() -- return a reverse iterator over the list
 |  
 |  __rmul__(self, value, /)
 |      Return self*value.
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(...)
 |      L.__sizeof__() -- size of L in memory, in bytes
 |  
 |  append(...)
 |      L.append(object) -> None -- append object to end
 |  
 |  clear(...)
 |      L.clear() -> None -- remove all items from L
 |  
 |  copy(...)
 |      L.copy() -> list -- a shallow copy of L
 |  
 |  count(...)
 |      L.count(value) -> integer -- return number of occurrences of value
 |  
 |  extend(...)
 |      L.extend(iterable) -> None -- extend list by appending elements from the iterable
 |  
 |  index(...)
 |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.
 |  
 |  insert(...)
 |      L.insert(index, object) -- insert object before index
 |  
 |  pop(...)
 |      L.pop([index]) -> item -- remove and return item at index (default last).
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(...)
 |      L.remove(value) -> None -- remove first occurrence of value.
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(...)
 |      L.reverse() -- reverse *IN PLACE*
 |  
 |  sort(...)
 |      L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None

In [23]:
nums.reverse()
In [24]:
nums
Out[24]:
[10, 0, 26, 34]
In [25]:
bin(65)
Out[25]:
'0b1000001'
In [28]:
bin(ord("A"))
Out[28]:
'0b1000001'
In [ ]: