Lists, Tuple, Sets and Dictionaries

Differences

Lists

Tuples

Sets

Dictionaries

A list is a collection of ordered data.

A tuple is an ordered collection of data.

A set is an unordered collection.

A dictionary is an unordered collection of data that stores data in key-value pairs.

Lists are mutable.

Tuples are immutable.

Sets are mutable and have no duplicate elements.

Dictionaries are mutable and keys do not allow duplicates.

Lists are declared with square braces.

Tuples are enclosed within parenthesis.

Sets are represented in curly brackets.

Dictionaries are enclosed in curly brackets in the form of key-value pairs.

Lists

How to create

list1=[1,4,"Gitam",6,"college"]
list2=[]  # creates an empty list
list3=list((1,2,3))
print(list1)
print(list2)
print(list3)

Operations on Lists

list1=["hello",1,4,8,"good"]
print(list1)
list1[0]="morning"  # assigning values ("hello" is replaced with "morning")
print(list1)
print(list1[4])
print(list1[-1]) # list also allow negative indexing
print(list1[1:4]) # slicing
list2=["apple","mango","banana"]
print(list1+list2) # list concatenation

Add and Remove items

The append() method adds a single item at the end of the list without modifying the original list.

The pop() method removes the item at the given index from the list and returns it.

list1=["apple","banana","grapes"]
list1.append("strawberry")   # strawberry is added to the list
print(list1)
list1.pop()  # removes the last element from the list
print(list1)
list1.pop()
print(list1)

Sort Items

The sort() method sorts the elements of a given list in a specific ascending or descending order.

Find Items

index() searches for a given element from the start of the list and returns the lowest index where the element appears.

Count

The count() method returns the number of times the specified element appears in the list.

Reverse

The reverse() method reverses the elements of the list.

Tuple

How to create

tuple1=(1,2,"college",9)
tuple2=() # creates an empty tuple
tuple3=tuple((1,3,5,9,"hello"))
print(tuple1)
print(tuple2)
print(tuple3)

Operations on Tuples

tuple1=("good",1,2,3,"morning")
print(tuple1)
print(tuple1[0])  # accessing values using indexing
#tuple1[1]="change"  # a value cannot be changed as they are immutable
tuple2=("orange","grapes")
print(tuple1+tuple2)  # tuples can be concatenated
tuple3=(1,2,3)
print(type(tuple3))

Add and Remove items

An element cannot be added to the tuple as it is immutable.

tuple1=(1,2,3,4)
# tuple1.pop()    tuple cannot be modified
# tuple1.append()  tuple cannot be modified
print(tuple1)

Sort Item

Though tuples are ordered, the elements cannot be sorted.

Find Items

Searches the tuple for a specified value and returns the position of where it was found.

Count

The count() method returns the number of times a specified value occurs in a tuple.

Reverse

The reverse() method is not defined in tuples, as they are unchangeable

Sets

How to create

set1={1,2,3,4,5,"hello","tup"}
set2={(1,8,"python",7)}
print(set1)
print(set2)

Operations on Sets

set1={1,2,3,4,5}
print(set1)
# set1[0]   sets are unordered, so it doesnot support indexing
set2={3,7,1,6,1} # sets doesnot allow duplicate values
print(set2)

Add and Remove items

The set add() method adds a given element to a set.

The pop() method removes a random item from the set.

set1={"water","air","food"}
set1.add("shelter")   # adds an element to the set at random position
print(set1)
set1.add("clothes")
print(set1)
set1.pop()  # removes random element from the set
print(set1)

Sort Items

Elements in the set cannot be sorted as they are unordered.

Find Items

The index of a particular element is not retrieved as they are unordered.

Count

There are no count() methods in sets as they do not allow any duplicates.

Reverse

The sets are unordered, which refrains from applying the reverse() method.

Dictionaries

How to create

dict1={"key1":"value1","key2":"value2"}
dict2={}   # empty dictionary
dict3=dict({1:"apple",2:"cherry",3:"strawberry"})
print(dict1)
print(dict2)
print(dict3)

Operations on Sets

dict1={"key1":1,"key2":"value2",3:"value3"}
print(dict1.keys())  # all the keys are printed
dict1.values() # all the values are printed
dict1["key1"]="replace_one"  # value assigned to key1 is replaced
print(dict1)
print(dict1["key2"])

Add and Remove items

The update() method updates the dictionary with the specified key-value pairs.

The pop() method removes the specified item from the dictionary.

dict1={"fruit1":"apple","fruit2":"banana","veg1":"tomato"}
dict1.update({"veg2":"brinjal"})
print(dict1)
dict1.update({"veg3":"chilli"})  # updates the dictionary at the end
print(dict1)
dict1.pop("veg2")
print(dict1)

Sort Items

sorted() method is used to sort the keys in the dictionary by default.

Find Items

The get() method returns the value of the item with the specified key.

Count

The count() method is not defined in the dictionary.

Reverse

The elements cannot be reversed, as the items in the dictionary are in the form of key-value pairs

Last updated