printing a list in python code example

Example 1: how to print items in a list in a single line python

>>> l = [1, 2, 3]
>>> print(' '.join(str(x) for x in l))
1 2 3
>>> print(' '.join(map(str, l)))
1 2 3

Example 2: print list in one line python

# using * operator
scores = [11, 12, 13, 14, 15, 16]
print(*scores)

Example 3: print string elements in list python

# list having all elements are strings
strings = ['string1', 'string2', 'string3', 'string4']
for string in strings:
    print(string)

Example 4: printing a list in python

items = [1,2,3,4,5,6]

#print with [] brackets separated by ','
print(items)

#print using operator
print(*items)

#print using for 
for i in items:
    print(i)
    
#print using for and len function
for i in range(len(items)):
    print(items[i])

#you can also add sep and end methods to the print statement accordingly

Example 5: print all elements in list python

# using for loop
scores = [11, 12, 13, 14, 15, 16]
for score in scores:
    print(score)

Example 6: how to print from a python list

list = ['a', 'b']
print(list[0])
#that will print 'a'
#print(list[1]) will print 'b'