Featured Post

Python Set Operations Explained: From Theory to Real-Time Applications

Image
A  set  in Python is an unordered collection of unique elements. It is useful when storing distinct values and performing operations like union, intersection, or difference. Real-Time Example: Removing Duplicate Customer Emails in a Marketing Campaign Imagine you are working on an email marketing campaign for your company. You have a list of customer emails, but some are duplicated. Using a set , you can remove duplicates efficiently before sending emails. Code Example: # List of customer emails (some duplicates) customer_emails = [ "alice@example.com" , "bob@example.com" , "charlie@example.com" , "alice@example.com" , "david@example.com" , "bob@example.com" ] # Convert list to a set to remove duplicates unique_emails = set (customer_emails) # Convert back to a list (if needed) unique_email_list = list (unique_emails) # Print the unique emails print ( "Unique customer emails:" , unique_email_list) Ou...

How to Unpack a List into Variables Quickly in Python

Here are two examples to unpack a list in Python. You can do it easily by using splat operator. The asterisk in python is called a Splat operator. Here are two splat operators - Single and Double. Below, you will find examples.

1. Single splat operator


Consider, for example, this code:
abc = [1,2,3,4]
print(abc) 

Here the output will be: [1, 2, 3, 4]

What if you didn't want the list output in list format? What if all you wanted was the list of values to be written to the output console? You could write them using a loop and one of the output functions, but Python prefers an easier way:

print(*abc) 1 2 3 4


Unpack List Splat Operators



2. Double splat operator

Here, I have written a function:

def func(x,y,z): 
      return x + y + z

print(func(**d))

It will show '6' as output. Since, I have assigned values for x,y, and z in a dictionary. So by using a double splat operator you assign values to the function.

d = { 
'x': 1, 
'y': 2, 
'z': 3 
}


Related posts

Comments

Popular posts from this blog

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)

Python placeholder '_' Perfect Way to Use it