-2
catNames = []

while True:
    print('Enter the name of cat ' + str(len(catNames) + 1) + ' (Or enter nothing to stop.):')
    name = input()
    if name == '': break
    catNames = catNames + [name] # list concatenation

print('The cat names are:')

for name in catNames:
    print(' ' + name)

Can someone please explain this. catNames = catNames + [name] # list concatenation

Vico
  • 579
  • 3
  • 13
  • list concatentation - https://stackoverflow.com/questions/1720421/how-to-concatenate-two-lists-in-python, you can add an element to the list by using `append` method – yash Oct 24 '17 at 16:50
  • **concatenate**: (verb) *link (things) together in a chain or series* – Martijn Pieters Oct 24 '17 at 16:50
  • Also see string concatenation, etc. And [wikipedia](https://en.wikipedia.org/wiki/Concatenation). Had you searched for 'list concatenation' on Google you'd have gotten plenty of answers; what research did you do and what did you find? How did those resources not help for you? – Martijn Pieters Oct 24 '17 at 16:51

2 Answers2

0

catNames is an empty list. By summing a list to another list ([name]) you get a combined list. such as:

ls1 = [1]
ls2 = [2]

ls3 = ls1 + ls2

print(ls3)

output:

[1, 2]
Vico
  • 579
  • 3
  • 13
0

If my understanding is correct, you are asking for clarification regarding list concatenation vs. the append() function.

List concatenation is the process of combining two or more lists to form one larger list that a variable can be set equal to.

Append() simply adds an item to a list. Here is an example of concatenation.

myList = [0, 2, 5]
myList2 = [1, 45, 78]
#list concatenation
myList3 = myList + myList2
#output if printed would be [0, 2, 5, 1, 45, 78]

VS. append which simply adds an item

lst = [0, 1, 2]
lst.append(3)
print(lst)
#outputs [0, 1, 2, 3]
ComedicChimera
  • 466
  • 1
  • 4
  • 15