Categories
Python Answers

How to rotate a list in Python?

Sometimes, we want to rotate a list in Python.

In this article, we’ll look at how to rotate a list in Python.

How to rotate a list in Python?

To rotate a list in Python, we can use the deque instance’s rotate method.

For instance, we write:

from collections import deque

items = deque([1, 2, 3])
items.rotate(1)
print(items)

We use the deque class with a list to create a deque.

Then we call rotate on it with 1 to move the last item to the first position.

Therefore, items is deque([3, 1, 2]) according to the console.

Conclusion

To rotate a list in Python, we can use the deque instance’s rotate method.

Categories
Python Answers

How to run the shell ‘cd’ command to change the working directory with Python?

Sometimes, we want to run the shell ‘cd’ command to change the working directory with Python.

In this article, we’ll look at how to run the shell ‘cd’ command to change the working directory with Python.

How to run the shell ‘cd’ command to change the working directory with Python?

To run the shell ‘cd’ command to change the working directory with Python, we can call the os.chdir method.

For instance, we write:

import os

path = './'
os.chdir(path)

to change the current working directory to path.

Conclusion

To run the shell ‘cd’ command to change the working directory with Python, we can call the os.chdir method.

Categories
Python Answers

How to print a single backslash with Python?

Sometimes, we want to print a single backslash with Python.

In this article, we’ll look at how to print a single backslash with Python.

How to print a single backslash with Python?

To print a single backslash with Python, we pass in '\\' into print.

For instance, we write:

print("\\")

And we see \ printed.

Conclusion

To print a single backslash with Python, we pass in '\\' into print.

Categories
Python Answers

How to create a cartesian product with Python Pandas?

Sometimes, we want to create a cartesian product with Python Pandas.

In this article, we’ll look at how to create a cartesian product with Python Pandas.

How to create a cartesian product with Python Pandas?

To create a cartesian product with Python Pandas, we can call the merge method.

For instance, we write:

from pandas import DataFrame

df1 = DataFrame({'col1': [1, 2], 'col2': [3, 4]})
df2 = DataFrame({'col3': [5, 6]})

df = df1.merge(df2, how='cross')
print(df)

to call df1.merge to merge data frtame df1 with df2.

We set how to 'cross' to create a cartesian product and assign the returned data frame to df.

Therefore, df is:

   col1  col2  col3
0     1     3     5
1     1     3     6
2     2     4     5
3     2     4     6

Conclusion

To create a cartesian product with Python Pandas, we can call the merge method.

Categories
Python Answers

How to convert hex string to int in Python?

Sometimes, we want to convert hex string to int in Python.

In this article, we’ll look at how to convert hex string to int in Python.

How to convert hex string to int in Python?

To convert hex string to int in Python, we can call the int function.

For instance, we write:

x = int("deadbeef", 16)
y = int("0xdeadbeef", 0)
print(x)
print(y)

We call int with the hex number with or without the 0x prefix.

If we skip the 0x prefix, then we need to pass in the base as the 2nd argument.

Otherwise, we can pass in 0 for the 2nd argument.

Therefore, x and y are both 3735928559.

Conclusion

To convert hex string to int in Python, we can call the int function.