-3

How do i make a new list containing existing list + a new string?

Const1 = "1000"
Const2 = "2000"
Const3 = "3000"
CONST_LIST = [Const1, Const2]
CONST_LIST_NEW = [CONST_LIST] + Const3 #Nogo
print CONST_LIST_NEW

Wish output..... ['1000', '2000', '3000']

khelwood
  • 55,782
  • 14
  • 81
  • 108
Klaus M
  • 3
  • 2
  • Possible duplicate of [Difference between append vs. extend list methods in Python](https://stackoverflow.com/questions/252703/difference-between-append-vs-extend-list-methods-in-python) – Chris Feb 26 '19 at 10:07

1 Answers1

0

If you look at the error the interpreter is giving you, you will see there is a TypeError: TypeError: can only concatenate list (not "str") to list.

So you need to conecatenate two lists, and as CONT_LIST is already a list, there is no need for square brackets around it. But your string needs to be in a new list, so just put [Const3]

Finally it looks like this:

Const1 = "1000"
Const2 = "2000"
Const3 = "3000"
CONST_LIST = [Const1, Const2]
CONST_LIST_NEW = CONST_LIST + [Const3]
print CONST_LIST_NEW
Marko
  • 733
  • 8
  • 21
  • Thx. But isnt that gonna give this outout? (['1000', '2000'], ['3000']) – Klaus M Feb 26 '19 at 10:16
  • For anyone else wondering, the output of `(['1000', '2000'], ['3000'])` is given when you leave `[CONT_LIST]`. A good explanation can be seen in @Chris comment in the question above. – Marko Feb 26 '19 at 12:39