Working with lists and sets in Python

By Abhinash & Priya Chetty on August 25, 2022

Python has sequence-appropriate operations that can be used to manipulate lists. These include concatenation, multiplication, and comparisons. Like lists, sets in Python are another useful type of unordered sequence to store values.

Concatenating lists in Python

Concatenation of lists is helpful when the items of the lists have to be processed in a similar manner. Two different lists can be combined together by using the ‘+’ operator.

alpha = ['a','b','c','d']
num = [1,2,3,4,5]
print(alpha + num)
#OUTPUT
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5]

Furthermore, to clone the list elements use ‘*’.

alpha = ['a','b','c','d']
num = [1,2,3,4,5]
c = alpha + num
print(c * 2)
#OUTPUT
['a', 'b', 'c', 'd', 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 1, 2, 3, 4, 5]

Changing lists to sets in Python

Sets are another data type in Python that stores unique values in an unordered sequence. The only difference is unlike lists, sets do not store multiple occurrences of values. Thus, when python lists are converted to sets of multiple occurrences of the same values are eliminated. As sets contain unique elements, applying unions and intersections to the data becomes easier.

a = ['a','b','c','c']
s = set(a)
print(s)
#OUTPUT
{'b', 'c', 'a'}
TIP

Sets can come very handy when you have to take out all similar occurrences of values in a list. You don’t have to use a loop to iterate through a list to find the unique elements.

The intersection of two sets in Python

The intersection of sets is a standard math operation that helps to identify common elements in two sets.

Intersection of two different skill sets
The intersection of two different skill sets
a = ['SPSS','Python','R','Tableau','MS Excel','MS Word','SQL']
b = ['PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL']
c = set(a) & set(b)
print(c) 
#OUTPUT
{'MS Word', 'MS Excel', 'SQL'}

Difference between the two sets

Different between sets in Python
Different between sets

The difference between the two sets is the set of all values that are there in one set and not in the other.

a = ['SPSS','Python','R','Tableau','MS Excel','MS Word','SQL']
b = ['PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL']
c = set(a) - set(b)
print(c) 
#OUTPUT
{'R', 'SPSS', 'Tableau', 'Python'}

Symmetric difference between the two sets

The symmetric difference between the two sets is the unique values that are not common in either of the sets.

Symmetric difference in sets in Python
The symmetric difference in sets
a = ['SPSS','Python','R','Tableau','MS Excel','MS Word','SQL']
b = ['PHP','Java','CSS','Git','HTML','MS Excel','MS Word','SQL']
c = set(a) ^ set(b)
print(c) 
#OUTPUT
{'R', 'SPSS', 'Java', 'CSS', 'HTML', 'Python', 'PHP', 'Git', 'Tableau'}

Discuss