Q:

Python program to print list elements in different ways

belongs to collection: Python List Programs

0

In this program – we are going to learn how can we print all list elements, print specific elements, print a range of the elements, print list multiple times (using * operator), print multiple lists by concatenating them, etc.

Syntax to print list in different ways:

    print (list)          	# printing complete list
    print (list[i])       	# printing ith element of list
    print (list[i], list[j])	# printing ith and jth elements
    print (list[i:j])     	# printing elements from ith index to jth index
    print (list[i:])      	# printing all elements from ith index
    print (list * 2)  		# printing list two times
    print (list1 + list2) 	# printing concatenated list1 & list2

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python code to print list elements

Here, we have two lists list1 and list2 with some of the elements (integers and strings), we are printing elements in the different ways.

# python program to demonstrate example of lists

# declaring & initializing two list
list1 = ["Amit", "Abhi", "Radib", 21, 22, 37]
list2 = [100, 200, "Hello", "World"]

print (list1)          		# printing complete list1
print (list1[0])       		# printing 0th (first) element of list1
print (list1[0], list1[1])	# printing first & second elements
print (list1[2:5])     		# printing elements from 2nd to 5th index
print (list1[1:])      		# printing all elements from 1st index
print (list2 * 2)  		# printing list2 two times
print (list1 + list2) 		# printing concatenated list1 & list2

Output

['Amit', 'Abhi', 'Radib', 21, 22, 37]
Amit
Amit Abhi
['Radib', 21, 22]
['Abhi', 'Radib', 21, 22, 37]
[100, 200, 'Hello', 'World', 100, 200, 'Hello', 'World']
['Amit', 'Abhi', 'Radib', 21, 22, 37, 100, 200, 'Hello', 'World']

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Program for Adding, removing elements in ... >>
<< Python | Program to declare and print a list...