|
| 1 | +# Let's fully understand what a 2d list is truly all about. |
| 2 | +# A 2d list is a two dimensional array that can hold multiple |
| 3 | +# 2d list array values under a single variable. For example: |
| 4 | + |
| 5 | +my_2d_list=['2d list0'],['2d list0'] |
| 6 | + |
| 7 | +print(my_2d_list[0][0]) |
| 8 | +print(my_2d_list[1][0]) |
| 9 | + |
| 10 | +# If you create a really long 2d list such as this example below, |
| 11 | +# you can force hard line-breaks, but you must use outer square |
| 12 | +# brackets '[]' to surround the entire 2d list values. Note: you |
| 13 | +# must use commas at the end of each 2d list array. |
| 14 | + |
| 15 | +# Example 1: |
| 16 | + |
| 17 | +my_2d_list=['2d list0'],['2d list0'],['2d list0'],['2d list0'],['2d list0'],['2d list0'],['2d list0'] |
| 18 | + |
| 19 | +print(my_2d_list[4][0]) |
| 20 | + |
| 21 | +# Example 2: |
| 22 | + |
| 23 | +my_2d_list=[ # use a hard line-break make the 2d list look neat and tidy. |
| 24 | + ['2d list0'],['2d list0'],['2d list0'], |
| 25 | + ['2d list0'],['2d list0'],['2d list0'], |
| 26 | + ['2d list0']] # use a hard line-break to add more values to the 2d list. |
| 27 | + |
| 28 | +print(my_2d_list[4][0]) |
| 29 | + |
| 30 | +# Create a multi-2d list array like this example below illustrates. |
| 31 | + |
| 32 | +my_multi_2d_list=['Value0','Value1','Value2'],['Value0','Value1','Value2'] |
| 33 | + |
| 34 | +print(my_multi_2d_list[0][0]) |
| 35 | +print(my_multi_2d_list[0][1]) |
| 36 | +print(my_multi_2d_list[0][2]) |
| 37 | + |
| 38 | +print(my_multi_2d_list[1][0]) |
| 39 | +print(my_multi_2d_list[1][1]) |
| 40 | +print(my_multi_2d_list[1][2]) |
| 41 | + |
| 42 | +# You can create as many multi-2d list array values as you please. |
| 43 | +# For example: |
| 44 | + |
| 45 | +my_multi_2d_list=[ |
| 46 | + ['Value0','Value1','Value2'], |
| 47 | + ['Value0','Value1','Value2','Value3'], |
| 48 | + ['Value0','Value1','Value2','Value3','Value4']] # neat and tidy |
| 49 | + |
| 50 | +print(my_multi_2d_list[0][2]) |
| 51 | +print(my_multi_2d_list[1][3]) |
| 52 | +print(my_multi_2d_list[2][4]) |
| 53 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 54 | +# Now, let's have some multi-2d list fun using a for-loop |
| 55 | +# and see what happens when we execute/run this multi-2d |
| 56 | +# list, for-loop example: |
| 57 | + |
| 58 | +my_multi_2d_list=[ |
| 59 | + ['Value0','Value1','Value2'], |
| 60 | + ['Value0','Value1','Value2'], |
| 61 | + ['Value0','Value1','Value2'], |
| 62 | + ['Value0','Value1','Value2']] # neat and tidy |
| 63 | + |
| 64 | +for i in my_multi_2d_list: |
| 65 | + print(i[0],i[1],i[2]) |
| 66 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 67 | +# Let's create a real, working multi-2d list to see what |
| 68 | +# they are truly all about in a real Python program scenario. |
| 69 | +# We will call our multi-2d list, 'names'. Use the (f') format |
| 70 | +# to make the 'print' statement easier to concatenate strings. |
| 71 | + |
| 72 | +names=[ |
| 73 | + ['Ron','Bob','Tom'], |
| 74 | + ['John','Mary','Terry'], |
| 75 | + ['Edie','Freddy','Teddy'], |
| 76 | + ['Charie','Marty','Harvey']] # neat and tidy |
| 77 | + |
| 78 | +for i in names: |
| 79 | + print(f'{i[0]}, {i[1]} and {i[2]} went to the store.') |
| 80 | + |
| 81 | +# Let's create a looping sentence tuple with our multi-2d list for-loop |
| 82 | +# example and see what happens when we execute/run this Python |
| 83 | +# program example below. |
| 84 | + |
| 85 | +names=[ |
| 86 | + ['Ron','Bob','Tom'], |
| 87 | + ['John','Mary','Terry'], |
| 88 | + ['Edie','Freddy','Teddy'], |
| 89 | + ['Charie','Marty','Harvey']] # neat and tidy |
| 90 | + |
| 91 | +sentence=( |
| 92 | + ('went home to have dinner.', |
| 93 | + 'went to the store to buy some food.', |
| 94 | + 'wanted some pizza for breakfast.', |
| 95 | + 'wanted computers for Christmas.', |
| 96 | + 'love their computers.')) |
| 97 | + |
| 98 | +for i in range(4): |
| 99 | + print(f'{names[i][0]}, {names[i][1]} \ |
| 100 | +and {names[i][2]} {sentence[i]}') |
0 commit comments