E-mail : support@tech2now.in

List and Tuple in Python

A list is a very important and powerful data structure in Python,
A single list can contain strings, integers, as well as other objects. Lists can also be used for implementing stacks and queues. Lists are mutable, i.e., They can be altered once declared. The elements of the list can be accessed using indexing and slicing operations.

lsta = [1,2,3,4,5]
lstb= ['a','b','c','d','e','f']
lstc = ['a',1,'b','abc']
print (lsta)
print (lstb)
print (lstc)
lsta.extend(lstb)
print (lsta)
lstc.append('xyz')
print (lstc)

# Tuples are similar to list but this is unmutable, we can not alter after declaration

tuplea = tuple((1,2,3,4,5))
tupleb= tuple(('a','b','c','d','e','f'))
tuplec = tuple(('a',1,'b','abc'))
print (tuplea)
print (tupleb)
print (tuplec)

try:
  tupleb.append('g')
  print(tupleb)
except:
  print('Error can not append')
OutPut:
[1, 2, 3, 4, 5]
['a', 'b', 'c', 'd', 'e', 'f']
['a', 1, 'b', 'abc']
[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'e', 'f']
['a', 1, 'b', 'abc', 'xyz']
(1, 2, 3, 4, 5)
('a', 'b', 'c', 'd', 'e', 'f')
('a', 1, 'b', 'abc')
Error can not append

GitHub Repository: https://github.com/dwivedianu01/Python/blob/main/ListTuple.py