-3

For some reason when I print a list like

list = []
list.append("0")
list.append("1")
print(list[0])

the output will be ["0"]

My actual code is a large block of text. Here's a link to the actual code: https://pastebin.com/Z54NfivR

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Estronoid
  • 5
  • 1
  • Welcome to Stack Overflow. "the output will be `["0"]`". No, it will not. `list[0]` in this code does not result in a list. Please read [ask] and [mre]. For the problem you are actually asking about, however, that is a common duplicate - please see the link. – Karl Knechtel Oct 02 '22 at 18:59
  • You can cut-and-paste those four lines of code into a Python session. The result is `0`. No brackets, no quotes. – Tim Roberts Oct 02 '22 at 19:04
  • Your actual problem is that you are using `random.choices` with `k=1`, which returns you a list of one item. If you only want one item, then use `random.choice`, which returns an element instead of a list. Or, since you want weights, use `random.choices(xxx, weights=yyy, k=1)[0]` to select the first item. – Tim Roberts Oct 02 '22 at 19:06

1 Answers1

0

Try this:

print(*list)

This essentially unpacks your list and its elements are treated as if they were separated by commas in the print function.

I used the name list because that was included in your example but it is a good practice to avoid using python commands as variable names.

Mario
  • 561
  • 3
  • 18
  • 1
    Yes, but that's not what he wanted. – Tim Roberts Oct 02 '22 at 19:06
  • 1
    It worked Thank you :D took me ages of trying different things and it was so simple lol and also I usually call my lists by some acronym I used the word list because of it makes it easier to understand – Estronoid Oct 04 '22 at 19:58