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'}
NOTES

Priya is the co-founder and Managing Partner of Project Guru, a research and analytics firm based in Gurgaon. She is responsible for the human resource planning and operations functions. Her expertise in analytics has been used in a number of service-based industries like education and financial services.

Her foundational educational is from St. Xaviers High School (Mumbai). She also holds MBA degree in Marketing and Finance from the Indian Institute of Planning and Management, Delhi (2008).

Some of the notable projects she has worked on include:

  • Using systems thinking to improve sustainability in operations: A study carried out in Malaysia in partnership with Universiti Kuala Lumpur.
  • Assessing customer satisfaction with in-house doctors of Jiva Ayurveda (a project executed for the company)
  • Predicting the potential impact of green hydrogen microgirds (A project executed for the Government of South Africa)

She is a key contributor to the in-house research platform Knowledge Tank.

She currently holds over 300 citations from her contributions to the platform.

She has also been a guest speaker at various institutes such as JIMS (Delhi), BPIT (Delhi), and SVU (Tirupati).

 

I am currently working as a Research Associate. My work is centered on Macroeconomics with modern econometric approach. Broadly, the methodological research focuses on Panel data and Times series data analysis for causal inference and prediction. I also served as a reviewer to Journals of Taylor & Francis Group, Emerald, Sage.

Discuss