Python program to print list elements in different ways

Printing list elements in Python: Here, we are going to learn how to print list elements in different ways? By IncludeHelp Last updated : June 21, 2023

Problem statement

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.

Print list elements in different ways

There can be multiple ways to print Python list elements using the print() statement. The following are some of the ways by which you can print the complete list or list elements:

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

Python program 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

The output of the above program will be:

['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']

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.