Le Lee
Le Lee

Reputation: 15

what is this python array syntax

There is this particular array python syntax that my University tutor uses and I can't understand it. I searched everywhere for it but haven't found anything remotely close. As you can see in the code, he used this : arrayb+= [arraya(m)][1]].

I am only confused about the syntax and fear that I may miss something important to the array topic. Running this code via python idle gives me a faint idea what it would do: only take the value of element at index 1 in array a. But is that everything to it?

Thank you in advance

n = int(input("Wie viele Wortpaare sollen eingegeben werden: "))
a = []

for i in range(1,n+1):
   print("Bitte geben Sie das "+str(i)+"te Wortpaar ein:")
  a += [[input(),input()]]

b = []
for m in range(0,len(a)):
  b += [a[m][1]]
  b += [a[m][0]]


c = []
for x in range(len(a)-1,-1,-1):
   c += a[x]

print(a)
print(b)
print(c)

Upvotes: 0

Views: 66

Answers (1)

CMMCD
CMMCD

Reputation: 360

a[m]      # Get the m'th element in a
a[m][1]   # a[m] was a indexable object, now get the first item from that object
[a[m][1]] # From the object at a[m][1] create a new list containing only that object
b += [a[m][1]] # Add that item to the list, similar result as b.append(a[m][1])

Example

>>> m = 2
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> b = [0, 0]

>>> a[m]
[7, 8, 9]
>>> a[m][1]
8
>>> [a[m][1]]
[8]

>>> b += [a[m][1]]
>>> b
[0, 0, 8]

Upvotes: 1

Related Questions