0

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)

1 Answer 1

1
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]
Sign up to request clarification or add additional context in comments.

4 Comments

Not literally "the same" as append - actually, worse than append in this case. something + something creates a new object, while append modifies the already existing one.
For lists += uses __iadd__ which modifies the list in place.
Ah, yes, sorry! Forgot there's a difference in python with iadd and add
thank you everyone , it is so much clear to me now. thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.