Skip to main content

Command Palette

Search for a command to run...

List vs Tuple in Python

Updated
1 min readView as Markdown
A

👋 Welcome to my Hashnode blog! I'm a tech enthusiast and cloud advocate with expertise in IT, backup solutions, and cloud technologies. Currently, I’m diving deep into Python and exploring its applications in automation and data management. I share insights, tutorials, and tips on all things tech, aiming to help fellow developers and enthusiasts. Join me on this journey of learning and innovation!

List are mutable and Tuples are immutable. Both can contain different data types.

If we want to add or remove any element in tuple then we will have to first convert it into list then apply the changes and have to convert back it to the tuples. However we can concatenate tuples.

l=[3,4,9,1,6]

l.append(10) # will add 10 in the list

l.sort() # sort in ascending order

print(l)

l.sort(reverse=True) # sort in descending order

print(l)

print(l.count(9)) # will count the no. of occurrence of 9 in list

l.reverse() # reverse the string

print(l)

print(l.index(3))

m=l.copy() # copy l list into m

m[0]=45

print(m)

l.insert(2,788) # it will insert 788 in 2nd index

print(l)

n=[100,200,500]

l.extend(n) # it will add n list to l

print(l)

k=l+m # concatenate the list

print(k)

tup=(1,2,3,4,5,"green",True)

print(type(tup))

print(tup[0])

print(tup[-2])

if 3 in tup:

print("yes")

tup2=tup[1:3]

print(tup2,tup)

More from this blog

PythonChallenge

11 posts

Welcome to my Python blog, where I share my journey of learning and exploring the versatile world of Python programming! 🌟

Let’s code, learn, and grow together in the world of Python programming!