DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

List in Python (1)

Buy Me a Coffee

*Memos:

  • My post explains the list with slicing and copy.
  • My post explains the useful functions for a list (1).
  • My post explains the useful functions for a list (2).
  • My post explains the shallow copy and deep copy of a list.
  • My post explains a tuple.
  • My post explains a set and copy.
  • My post explains a dictionary, the dictionary with keying and copy.
  • My post explains an iterator (1).
  • My post explains variable assignment.

A list:

  • is an ordered collection to use non-huge data not to get MemoryError.
  • allows duplicated elements (even with different types).
  • is mutable so it can be changed.
  • can have any mixed types of elements.
  • can be enlarged with * and a number.
  • can be created by [], list() with or without a list, tuple, set, dictionary, iterator, string or range() or by a list comprehension.
  • can be used with len() to get the length.
  • can be accessed and changed by indexing or slicing.
  • can be continuously used through multiple variables.
  • can be copied to refer to a different list.

A list is for non-huge data otherwise it gets MemoryError.


[] can create a list as shown below:

v = [] # Empty 1D list v = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] # 1D list v = ['a', 'b', 'c', 'a', 'b', 'c'] # 1D list v = ['a', 'b', 'c'] * 3 # 1D list v = ['a', 'b', 'c', 'd', ['e', 'f', 'g', 'h']] # 2D list v = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']]# 2D list v = [['a', 'b', 'c']] * 3 # 2D list v = [['a', 'b', 'c', 'd'], [['e', 'f'], ['g', 'h']]] # 3D list v = [[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]] # 3D list v = [[['a', 'b', 'c']]] * 3 # 3D list v = [1, 1.0, 1.0+0.0j, True] v = ['a', 2, 2.3, 2.3+4.5j, True, [2, 3], (2, 3), {2, 3}, {'a':'A'}] # No error 
Enter fullscreen mode Exit fullscreen mode

A list is an ordered collection as shown below:

v = ['a', 'b', 'c', 'd', 'e'] print(v) # ['a', 'b', 'c', 'd', 'e'] 
Enter fullscreen mode Exit fullscreen mode

A list allows duplicated elements (even with different types) as shown below:

v = ['a', 'b', 'c', 'a', 'b', 'c'] print(v) # ['a', 'b', 'c', 'a', 'b', 'c'] 
Enter fullscreen mode Exit fullscreen mode
v = [1, 1.0, 1.0+0.0j, True] print(v) # [1, 1.0, (1+0j), True] 
Enter fullscreen mode Exit fullscreen mode

A list can have any mixed types of elements as shown below:

v = ['a', 2, 2.3, 2.3+4.5j, True, [2, 3], (2, 3), {2, 3}, {'a':'A'}] print(v) # ['a', 2, 2.3, (2.3+4.5j), True, [2, 3], (2, 3), {2, 3}, {'a': 'A'}] 
Enter fullscreen mode Exit fullscreen mode

A list can be enlarged with * and a number as shown below:

1D list:
v1 = ['a', 'b', 'c'] * 3 v2 = [0] * 3 print(v1) # ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] print(v2) # [0, 0, 0] 
Enter fullscreen mode Exit fullscreen mode
2D list:
v1 = [['a', 'b', 'c']] * 3 v2 = [[0]] * 3 print(v1) # [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']] print(v2) # [[0], [0], [0]] 
Enter fullscreen mode Exit fullscreen mode
3D list:
v1 = [[['a', 'b', 'c']]] * 3 v2 = [[[0]]] * 3 print(v1) # [[['a', 'b', 'c']], [['a', 'b', 'c']], [['a', 'b', 'c']]] print(v2) # [[[0]], [[0]], [[0]]] 
Enter fullscreen mode Exit fullscreen mode

list() can create a list with or without a list, tuple, set, dictionary, iterator, string or range() as shown below:

*Memos:

  • The 1st argument is iterable(Optional-Type:iterable).
  • Don't use iterable=.
v = list() # Empty list  print() # [] 
Enter fullscreen mode Exit fullscreen mode
v = ['a', 'b', 'c', 'd', 'e'] # List  print(list(v)) # ['a', 'b', 'c', 'd', 'e'] 
Enter fullscreen mode Exit fullscreen mode
v = ('a', 'b', 'c', 'd', 'e') # Tuple  print(list(v)) # ['a', 'b', 'c', 'd', 'e'] 
Enter fullscreen mode Exit fullscreen mode
v = {'a', 'b', 'c', 'd', 'e'} # Set  print(list(v)) # ['c', 'a', 'd', 'b', 'e'] 
Enter fullscreen mode Exit fullscreen mode
v = {'name': 'John', 'age': 36, 'gender': 'Male'} # Dictionary  print(list(v)) print(list(v.keys())) # ['name', 'age', 'gender']  print(list(v.values())) # ['John', 36, 'Male']  print(list(v.items())) # [('name', 'John'), ('age', 36), ('gender', 'Male')] 
Enter fullscreen mode Exit fullscreen mode
v = iter(['a', 'b', 'c', 'd', 'e']) # Iterator  print(list(v)) # ['a', 'b', 'c', 'd', 'e'] 
Enter fullscreen mode Exit fullscreen mode
v = 'Hello' # String  print(list(v)) # ['H', 'e', 'l', 'l', 'o'] 
Enter fullscreen mode Exit fullscreen mode
v = range(5) print(list(v)) # [0, 1, 2, 3, 4] 
Enter fullscreen mode Exit fullscreen mode

A list comprehension can create a list as shown below:

v = [x**2 for x in range(6)] print(v) # [0, 1, 4, 9, 16, 25] 
Enter fullscreen mode Exit fullscreen mode

Be careful, a huge list gets MemoryError as shown below:

v = ['a', 'b', 'c'] * 1000000000 # MemoryError 
Enter fullscreen mode Exit fullscreen mode
v = range(100000000) print(list(v)) # MemoryError 
Enter fullscreen mode Exit fullscreen mode

A list can be used with len() to get the length as shown below:

v = ['a', 'b', 'c', 'd', 'e'] print(len(v)) # 5 
Enter fullscreen mode Exit fullscreen mode

You can access and change a list by indexing as shown below. *Indexing can be done with one or more [index]:

v = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] # 1D list  print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]) print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1]) # a b c d e f g h  v[1] = 'B' v[-7] = 'B' print(v) # ['a', 'B', 'c', 'd', 'e', 'f', 'g', 'h'] 
Enter fullscreen mode Exit fullscreen mode
v = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']] # 2D list  print(v[0], v[1]) print(v[-2], v[-1]) # ['a', 'b', 'c', 'd'] ['e', 'f', 'g', 'h']  print(v[0][0], v[0][1], v[0][2], v[0][3], v[1][0], v[1][1], v[1][2], v[1][3]) print(v[-2][-4], v[-2][-3], v[-2][-2], v[-2][-1], v[-1][-4], v[-1][-3], v[-1][-2], v[-1][-1]) # a b c d e f g h  v[0][1] = 'B' v[-2][-3] = 'B' v[1] = ['E', 'F', 'G', 'H'] v[-1] = ['E', 'F', 'G', 'H'] print(v) # [['a', 'B', 'c', 'd'], ['E', 'F', 'G', 'H']] 
Enter fullscreen mode Exit fullscreen mode
v = [[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]] # 3D list  print(v[0], v[1]) print(v[-2], v[-1]) # [['a', 'b'], ['c', 'd']] [['e', 'f'], ['g', 'h']]  print(v[0][0], v[0][1], v[1][0], v[1][1]) print(v[-2][-2], v[-2][-1], v[-1][-2], v[-1][-1]) # ['a', 'b'] ['c', 'd'] ['e', 'f'] ['g', 'h']  print(v[0][0][0], v[0][0][1], v[0][1][0], v[0][1][1], v[1][0][0], v[1][0][1], v[1][1][0], v[1][1][1]) print(v[-2][-2][-2], v[-2][-2][-1], v[-2][-1][-2], v[-2][-1][-1], v[-1][-2][-2], v[-1][-2][-1], v[-1][-1][-2], v[-1][-1][-1]) # a b c d e f g h  v[0][0][1] = 'B' v[-2][-2][-1] = 'B' v[0][1] = 'C' v[-2][-1] = 'C' v[1] = ['D', ['E', 'F']] v[-1] = ['D', ['E', 'F']] print(v) # [[['a', 'B'], 'C'], ['D', ['E', 'F']]] 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)