Python list, tuple, and set.

Martin Jun Cho
2 min readJan 22, 2022

The main key of the Python list, tuple, set to tuple allows us to store multiple values in a single variable. A tuple is very similar to a list but instead of using square brackets, you use normal brackets. The key difference between lists and tuples is that you can’t modify a tuple, whereas you can add and remove which is called immutable. And the key difference between a set and the other two is that while you can add and remove elements from a set you can’t have duplicate elements.

l = [“Apple”,”Kiwi”,”Kiwi”,”Mango”] #List with squre bracket /mutable and duplicate is acceptable.t = (“Apple”,”Kiwi”,”Kiwi”,”Mango”) #Tuple with normal bracket /immutable duplicate is acceptable.s = {“Apple”,”Kiwi”,”Mango”} #Set with curly braces duplicate is unacceptable.

So you couldn’t have Kannan twice in the set. Also, lists and tuples keep the order of the elements. So whenever you print out this list, it will always have Jun then Kannan, and then Kevin in that order, but sets don’t necessarily keep the order. This means if you want to access individual variables on lists or tuples, you can simply access them by using subscription notation.

print(t[1]) // It will give you the element with index 1 which is “Kiwi”.

If you want to add elements, you can use “append”(means add at the end) on sets, tuple, and add on set. you might wonder why they use different methods because the set doesn’t have the order and an “end”. removal is also pretty effortless. simply using remove allows you to remove variables you want.

ex)

l.append(“Banana”) or s.add(“Banana”) // additionl.remove(“Apple”) // removal

Thank you.

--

--

Martin Jun Cho

As a software engineer, I am embarking on a long and exciting programming journey driven by my genuine curiosity and passion.