+ 1

hey, when i code x = [1,2,3] print(x) ->>> I get [1,2,3] When i code x = [1,2,3] print(x[]) I get an error How do i print 1,2,3 on my console without the '[]' around my numbers? how do i print my whole list?

4th Sep 2016, 2:23 PM
Oliver
6 Answers
+ 2
you define list x = [1, 2, 3] , so when you print (x), it'll print a whole list of x = [1, 2, 3]. if you code print (x[]) , you need to assing index of list that you want to print but not more than length of list x if you want to print each item in the whole list , you need to use loop like for , example x = [1, 2, 3] #x has 3 members #x'lenght = 3 for i in range(len(x)): #range(len(x)) = range(3) = [0, 1, 2] print (x[i]) #print each value of x by index i #this'll print and get new line #print (x[i] , end = " ") #this'll print with the same line
4th Sep 2016, 4:10 PM
beauty1234
+ 1
Paranoid has a point, but his code is wrong (missing incrementing i) and non pythonic. for item in x: print(item)
4th Sep 2016, 3:49 PM
trueneu
trueneu - avatar
+ 1
you're right! I'll edit it!!
4th Sep 2016, 3:58 PM
Paranoid Nemo
Paranoid Nemo - avatar
0
x[1] print 1 x[2] print 2 x[3] print 3 its an array lile a table
4th Sep 2016, 2:58 PM
Tn Hacker
Tn Hacker - avatar
0
it's a list so you always will have the value wrapped into []. you could print the single values without brackets with a for loop i = 0 for i < len(x) print(x[i]) i += 1
4th Sep 2016, 3:03 PM
Paranoid Nemo
Paranoid Nemo - avatar
- 2
Paranoid, it also should be while, not for. And if it's for, it's for i in range(len(x)) :) Beauty1234's answer is the best.
4th Sep 2016, 4:14 PM
trueneu
trueneu - avatar