Python List of Lists

From GM-RKB
Jump to navigation Jump to search

A Python List of Lists is an array of array composed of Python lists.

  • Example(s):
    • LoL = [[3, 'a', 4], ['b', 5, 11], [1, 7, 3.1]]
      LoL2[2][1]
      LoL2[1:]
    • LoL = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]
      [sublist[:3] for sublist in LoL]
      OUTPUT: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
    • empty_LoL=[[] for i in range(4)] results in [[], [], [], []]
  • Counter-Example(s):
  • See: Python Matrix.


References

2010

  • http://stackoverflow.com/questions/11487049/python-list-of-lists
    • QUOTE: Lists are a mutable type - in order to create a copy (rather than just passing the same list around), you need to do so explicitly:
      listoflists.append((list[:], list[0]))
      However, list is already the name of a Python built-in - it'd be better not to use that name for your variable.
listoflists = []
a_list = []
for i in range(0,10):
   a_list.append(i)
   if len(a_list)>3:
       a_list.remove(a_list[0])
       listoflists.append((list(a_list), a_list[0]))
print listoflists