Categories
Python Answers

How to unpack tuples in for loops with Python?

Sometimes, we want to unpack tuples in for loops with Python.

In this article, we’ll look at how to unpack tuples in for loops with Python.

How to unpack tuples in for loops with Python?

To unpack tuples in for loops with Python, we separate the tuple entries with commas.

For instance, we write:

x = [(1, 2), (3, 4), (5, 6)]
for a, b in x:
    print(a, b)

We have the list x which is a list of tuples.

Then we loop through each entry in x and unpack the tuples entry with a, b.

In the loop body, we print out the tuple entries.

And so we get:

1 2
3 4
5 6

printed.

Conclusion

To unpack tuples in for loops with Python, we separate the tuple entries with commas.

Categories
Python Answers

How to loop through DataFrames with Python Pandas?

Sometimes, we want to loop through DataFrames with Python Pandas.

In this article, we’ll look at how to loop through DataFrames with Python Pandas.

How to loop through DataFrames with Python Pandas?

To loop through DataFrames with Python Pandas, we can use the df.iterrows method.

For instance, we write:

import pandas as pd

df = pd.DataFrame({'col1': [1, 2, 3]})
for index, row in df.iterrows():
    print(index, row)

We create a DataFrame with pd.DataFrame with a dictionary as its argument.

Then we loop through the DataFrame rows returned by the df.iterrows method.

In the loop body, we unpack the index and row and print them.

Then we get:

0 col1    1
Name: 0, dtype: int64
1 col1    2
Name: 1, dtype: int64
2 col1    3
Name: 2, dtype: int64

from the print output.

Conclusion

To loop through DataFrames with Python Pandas, we can use the df.iterrows method.

Categories
Python Answers

How to extract the extension from the filename in Python?

Sometimes, we want to extract the extension from the filename in Python.

In this article, we’ll look at how to extract the extension from the filename in Python.

How to extract the extension from the filename in Python?

To extract the extension from the filename in Python, we can use the os.path.splittext method.

For instance, we write:

import os

filename, file_extension = os.path.splitext('/path/to/somefile.ext')
print(file_extension)

We call os.path.splittext with the path string we want to extract the file extension from.

Then we take the file_extension from the returned tuple.

Therefore, file_extension is '.ext'.

Conclusion

To extract the extension from the filename in Python, we can use the os.path.splittext method.

Categories
Python Answers

How to interleave multiple lists of the same length in Python?

Sometimes, we want to interleave multiple lists of the same length in Python.

In this article, we’ll look at how to interleave multiple lists of the same length in Python.

How to interleave multiple lists of the same length in Python?

To interleave multiple lists of the same length in Python, we can use list comprehension and zip.

For instance, we write:

l1 = [1, 2]
l2 = [3, 4]
l3 = [5, 6]
lists = [l1, l2, l3]
l = [val for tup in zip(*lists) for val in tup]
print(l)

We have 3 lists l1, l2, and l3.

And then we put them into the lists list.

Then to interleave all the lists, we call zip with all the lists in lists as arguments.

Then we use [val for tup in zip(*lists) for val in tup] to interleave the elements by taking the items from the tuples.

Therefore, l is [1, 3, 5, 2, 4, 6].

Conclusion

To interleave multiple lists of the same length in Python, we can use list comprehension and zip.

Categories
Python Answers

How to parse dates with -0400 time zone string in Python?

Sometimes, we want to parse dates with -0400 time zone string in Python.

In this article, we’ll look at how to parse dates with -0400 time zone string in Python.

How to parse dates with -0400 time zone string in Python?

To parse dates with -0400 time zone string in Python, we can use the dateutil.parser module.

For instance, we write:

from dateutil.parser import parse

d = parse('2020/05/13 19:19:30 -0400')
print(d)

We call parse with the date string including the time zone.

And then we assign the returned result to d.

Therefore, d is '2020-05-13 19:19:30-04:00'.

Conclusion

To parse dates with -0400 time zone string in Python, we can use the dateutil.parser module.