Categories
Python Answers

How to concatenate str and int objects with Python?

Sometimes, we want to concatenate str and int objects with Python.

In this article, we’ll look at how to concatenate str and int objects with Python.

How to concatenate str and int objects with Python?

To concatenate str and int objects with Python, we can use the string’s format method, string interpolation or an f-string.

For instance, we write:

things = 3
s1 = 'You have %d things.' % things
s2 = 'You have {} things.'.format(things)
s3 = f'You have {things} things.'
print(s1)
print(s2)
print(s3)

We use string interpolate to create s1.

%d is a placeholder for a number within the string.

Then we put the variables we interpolate after the % sign.

To create s2, we put curly braces in the string for the placeholder.

Then we call format on the string with the variables we want to interpolate.

Finally, we create s3 with the f prefix and the variable right inside the string surrounded by curly braces.

f-strings are available since Python 3.6.

Therefore, s1, s2, and s3 are all 'You have 3 things.'.

Conclusion

To concatenate str and int objects with Python, we can use the string’s format method, string interpolation or an f-string.

Categories
Python Answers

How to transpose a list of lists with Python?

Sometimes, we want to transpose a list of lists with Python.

In this article, we’ll look at how to transpose a list of lists with Python.

How to transpose a list of lists with Python?

To transpose a list of lists with Python, we can use the map function with the itertools.zip_longest method.

itertools.zip_longest lets us transpose any nested arrays including jagged ones.

For instance, we write:

import itertools

l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
t = list(map(list, itertools.zip_longest(*l, fillvalue=None)))
print(t)

We call map with list and the array created by itertools.zip_longest which takes the item from each nested array and returned.

fillvalue is set to None so that lists that are shorter than the longest one in the list will have None added to them so that they match the length of the longest list.

Then we call list to convert the returned map with the transposed nested lists back to a nested list.

Therefore, t is [[1, 4, 7], [2, 5, 8], [3, 6, 9]].

Conclusion

To transpose a list of lists with Python, we can use the map function with the itertools.zip_longest method.

itertools.zip_longest lets us transpose any nested arrays including jagged ones.

Categories
Python Answers

How to read a big file in Python?

Sometimes, we want to read a big file in Python.

In this article, we’ll look at how to read a big file in Python.

How to read a big file in Python?

To read a big file in Python, we can call the readlines method on the opened file object with the buffer size.

For instance, we write:

BUF_SIZE = 100

bigfile = open('bar.txt', 'r')
tmp_lines = bigfile.readlines(BUF_SIZE)
while tmp_lines:
    print(tmp_lines)
    tmp_lines = bigfile.readlines(BUF_SIZE)

to call open with the file path of the file and 'r' to open the file with read permission.

Then we call bigfile.readlines with the BUF_SIZE buffer size.

BUF_SIZE is in bytes.

Next, we have a while loop that runs while tmp_lines isn’t None.

In the while loop, we call bigfile_readlines to read the next chunk in the buffer.

Conclusion

To read a big file in Python, we can call the readlines method on the opened file object with the buffer size.

Categories
Python Answers

How to find all occurrences of an element in a list with Python?

Sometimes, we want to find all occurrences of an element in a list with Python.

In this article, we’ll look at how to find all occurrences of an element in a list with Python.

How to find all occurrences of an element in a list with Python?

To find all occurrences of an element in a list with Python, we can use list comprehension.

For instance, we write:

l = [1, 2, 3, 4, 3, 2, 5, 6, 7]
indexes = [i for i, val in enumerate(l) if val == 3]
print(indexes)

We use enumerate to return an iterator with the tuples for the index and value of each l array entry.

Then we get the i index of each entry if their value is 3 by using the condition val == 3.

And then we assign the array of indexes of the l array where its values is 3 to indexes.

Therefore, indexes is [2, 4].

Conclusion

To find all occurrences of an element in a list with Python, we can use list comprehension.

Categories
Python Answers

How to copy a dictionary and only edit the copy with Python?

Sometimes, we want to copy a dictionary and only edit the copy with Python.

In this article, we’ll look at how to copy a dictionary and only edit the copy with Python.

How to copy a dictionary and only edit the copy with Python?

To copy a dictionary and only edit the copy with Python, we can use the dict function or the dictionary’s copy method.

For instance, we write:

dict1 = {'a': 1, 'b': 2}

c1 = dict(dict1)
c2 = dict1.copy()

print(c1)
print(c2)

to call dict with dict1 to return a copy of dict1.

Likewise, we call dict1.copy to return a copy of dict1.

Therefore, c1 and c2 are both {'a': 1, 'b': 2}.

Conclusion

To copy a dictionary and only edit the copy with Python, we can use the dict function or the dictionary’s copy method.