Categories
Python Answers

How to find the common elements between 2 lists with Python?

Sometimes, we want to find the common elements between 2 lists with Python.

In this article, we’ll look at how to find the common elements between 2 lists with Python.

How to find the common elements between 2 lists with Python?

To find the common elements between 2 lists with Python, we can convert the first list to a set and use the set’s intersection method.

For instance, we write:

list1 = [1, 2, 3, 4, 5, 6]
list2 = [3, 5, 7, 9]
intersection = list(set(list1).intersection(list2))
print(intersection)

We have 2 lists list1 and list2 and we want to get the intersection between them.

To do this, we call set with list1 to convert it to a set.

Then we can call intersection on it with list2 to return a set that has values from both lists.

Finally, we call list to return a list by converting it from the intersection set.

Therefore, intersection is [3, 5].

Conclusion

To find the common elements between 2 lists with Python, we can convert the first list to a set and use the set’s intersection method.

Categories
Python Answers

How to rename a dictionary key with Python?

Sometimes, we want to rename a dictionary key with Python.

In this article, we’ll look at how to rename a dictionary key with Python.

How to rename a dictionary key with Python?

To rename a dictionary key with Python, we can use the dictionary’s pop method.

For instance, we write:

d = {0: 0, 1: 1, 2: 2, 3: 3}
k_old = 0
k_new = 4
d[k_new] = d.pop(k_old)
print(d)

We have the dictionary d, and we want to change the key of the first entry from 0 to 4.

To do this, we call d.pop with k_old to remove the the entry with key 0.

And then we set the entry with key k_new to the dictionary entry value returned by pop, which is the value 0.

Therefore, d is {1: 1, 2: 2, 3: 3, 4: 0} according to the print output.

Conclusion

To rename a dictionary key with Python, we can use the dictionary’s pop method.

Categories
Python Answers

How to capitalize the first letter of each word in a string with Python?

Sometimes, we want to capitalize the first letter of each word in a string with Python.

In this article, we’ll look at how to capitalize the first letter of each word in a string with Python.

How to capitalize the first letter of each word in a string with Python?

To capitalize the first letter of each word in a string with Python, we can use the string’s title method.

For instance, we write:

s = "hello world".title()
print(s)

We call title on "hello world" to return the string with the first letter of each word capitalized.

And then we assign the returned string to s.

Therefore, s is 'Hello World'.

Conclusion

To capitalize the first letter of each word in a string with Python, we can use the string’s title method.

Categories
Python Answers

How to compute list difference with Python?

Sometimes, we want to compute list difference with Python.

In this article, we’ll look at how to compute list difference with Python.

How to compute list difference with Python?

To compute list difference with Python, we can use the - operator with sets.

For instance, we write:

A = [1, 2, 3, 4]
B = [2, 5]

x = list(set(A) - set(B))
print(x)

We create sets from A and B with the set functions.

Then we get the difference between the 2 sets with the - operator.

Finally, we convert the returned set difference back to a list with list and assign that to x.

Therefore, x is [1, 3, 4].

Conclusion

To compute list difference with Python, we can use the - operator with sets.

Categories
Python Answers

How to round up a number with Python?

Sometimes, we want to round up a number with Python.

In this article, we’ll look at how to round up a number with Python.

How to round up a number with Python?

To round up a number with Python, we can use the math.ceil method.

For instance, we write:

import math

print(int(math.ceil(5.2)))

Then we see 6 is printed since we call math.ceil to round 5.2 up to the nearest integer.

And then we use int to convert the float returned by math.ceil to an integer.

Conclusion

To round up a number with Python, we can use the math.ceil method.