Categories
Python Answers

How to do an element-wise addition of 2 lists with Python?

Sometimes, we want to do an element-wise addition of 2 lists with Python.

In this article, we’ll look at how to do an element-wise addition of 2 lists with Python.

How to do an element-wise addition of 2 lists with Python?

To do an element-wise addition of 2 lists with Python, we can use the operator.add method.

For instance, we write:

from operator import add

list1 = [1, 2, 3]
list2 = [4, 5, 6]
added = list(map(add, list1, list2))
print(added)

We call map with add and list1 and list2 to add the entries in list1 and list2 element-wise.

Then we call list to convert the returned iterable object back to a list.

Therefore, added is [5, 7, 9].

Conclusion

To do an element-wise addition of 2 lists with Python, we can use the operator.add method.

Categories
Python Answers

How to get the first item from an iterable that matches a condition with Python?

Sometimes, we want to get the first item from an iterable that matches a condition with Python.

In this article, we’ll look at how to get the first item from an iterable that matches a condition with Python.

How to get the first item from an iterable that matches a condition with Python?

To get the first item from an iterable that matches a condition with Python, we can use the next function.

For instance, we write:

a = [1, 2, 3, 4, 5, 6]
first = next(x for x in a if x > 3)
print(first)

We define the list a that has some integers in it.

And we get the all the integers that’s bigger than 3 with x for x in a if x > 3 and return the list.

Finally, we get the first one on the returned list with next and assign it to first.

Therefore, first is 4.

Conclusion

To get the first item from an iterable that matches a condition with Python, we can use the next function.

Categories
Python Answers

How to programmatically set an attribute with Python?

Sometimes, we want to programmatically set an attribute with Python.

In this article, we’ll look at how to programmatically set an attribute with Python.

How to programmatically set an attribute with Python?

To programmatically set an attribute with Python, we can use the setattr function.

For instance, we write:

class C:
    foo = 1


c = C()
setattr(c, 'bar', 2)

print(c.bar)

We have the C class with the foo attribute.

Then we create an instance of C and assign it to c.

Next, we add the bar attribute to c with setattr(c, 'bar', 2).

Therefore, c.bar is now 2.

Conclusion

To programmatically set an attribute with Python, we can use the setattr function.

Categories
Python Answers

How to search for a string in text files with Python?

Sometimes, we want to search for a string in text files with Python.

In this article, we’ll look at how to search for a string in text files with Python.

How to search for a string in text files with Python?

To search for a string in text files with Python, we can use the in operator.

For instance, if we have the following text file:

example.txt

blablabla

Then we write:

with open('example.txt') as f:
    if 'blabla' in f.read():
        print("true")

We call open with the path of the text file to open.

Then we call f.read to read the file’s contents into a string.

And then we use the in operator to check if the text we’re looking for is in the string returned by read.

Since this is True, we see 'true' printed.

Conclusion

To search for a string in text files with Python, we can use the in operator.

Categories
Python Answers

How to split a string by a delimiter in Python?

Sometimes, we want to split a string by a delimiter in Python.

In this article, we’ll look at how to split a string by a delimiter in Python.

How to split a string by a delimiter in Python?

To split a string by a delimiter in Python, we can use the string’s split method.

For instance, we write:

a = "MATCHES__STRING".split("__")
print(a)

We call split on the string with the separator we want to split the string with as the argument.

Then we assign the returned list to a.

Therefore, a is ['MATCHES', 'STRING'].

Conclusion

To split a string by a delimiter in Python, we can use the string’s split method.