Working with lists and sets in Python

By Abhinash Jena & 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'}
NOTES

I am an interdisciplinary educator, researcher, and technologist with over a decade of experience in applied coding, educational design, and research mentorship in fields spanning management, marketing, behavioral science, machine learning, and natural language processing. I specialize in simplifying complex topics such as sentiment analysis, adaptive assessments and data visualizatiion. My training approach emphasizes real-world application, clear interpretation of results and the integration of data mining, processing, and modeling techniques to drive informed strategies across academic and industry domains.

I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them. 

Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here

I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal. 

 

Discuss