756 Answered Questions for the topic Python

06/14/19

How to concatenate two lists in Python?

How do I concatenate two lists in Python?Example: listone = [1, 2, 3] listtwo = [4, 5, 6] Expected outcome: >>> joinedlist [1, 2, 3, 4, 5, 6]
Python Termination

06/14/19

Terminating a Python script?

I am aware of the `die()` command in PHP which stops a script early.How can I do this in Python?
Python Dictionary Merge

06/14/19

How to merge two dictionaries in a single expression?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The `update()` method would be what I need, if it returned its result instead... more

06/14/19

How can I do a line break (line continuation) in Python?

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?For example, adding a bunch of strings, e = 'a' + 'b' + 'c' + 'd'and have it in two... more

06/14/19

Use different Python version with virtualenv?

I have a Debian system currently running with python 2.5.4. I got virtualenv properly installed, everything is working fine. Is there a possibility that I can use a virtualenv with a different... more

06/14/19

How do you change the size of figures drawn with matplotlib?

How do you change the size of figure drawn with matplotlib?

Why does Python code run faster in a function?

def main():  for i in xrange(10**8):   pass main()This piece of code in Python runs in (Note: The timing is done with the time function in BASH in Linux.) real 0m1.841s ... more
Python String List Join

06/14/19

Python join: why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world"Than this: my_list = ["Hello", "world"] ... more

06/14/19

Removing duplicates in lists?

Pretty much I need to write a program to check if a list has any duplicates and if it does it removes them and returns a new list with the items that werent duplicated/removed. This is what I have... more
Python Setup.py Pypi

06/14/19

python setup.py uninstall?

I have installed a python package with `python setup.py install`.How do I uninstall it?
Python Constants

06/13/19

How do I create a constant in Python?

Is there a way to declare a constant in Python? In Java we can create constant values in this manner:<!-- language: lang-java --> public static final String CONST_NAME = "Name";What is the... more
Python Ide Editor

06/13/19

What IDE to use for Python?

What IDEs ("GUIs/editors") do others use for Python coding?
Python Pandas Dataframe

06/13/19

Get list from pandas DataFrame column headers?

I want to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I won't know how many columns there will be or what they will be called.For example,... more
Python Dictionary

06/12/19

Check if a given key already exists in a dictionary?

I wanted to test if a key exists in a dictionary before updating the value for the key.I wrote the following code: if 'key1' in dict.keys(): print "blah" else: print "boo"I think... more

06/11/19

What are the differences between type() and isinstance()?

What are the differences between these two code fragments?Using `type()`: import types if type(a) is types.DictType: do_something() if type(b) in types.StringTypes: ... more

How do I check if a string is a number (float)?

What is the best possible way to check if a string can be represented as a number in Python? The function I currently have right now is: def is_number(s): try: float(s) ... more

06/10/19

What is Tensorflow Python?

06/10/19

Are static class variables possible?

Is it possible to have static class variables or methods in python? What syntax is required to do this?
Python Coding Style

06/08/19

Single quotes vs. double quotes in Python?

According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
Python Json

06/08/19

How do I write JSON data to a file?

I have JSON data stored in the variable `data`.I want to write this to a text file for testing so I don't have to grab the data from the server each time.Currently, I am trying this: obj =... more

06/08/19

Why does comparing strings using either '==' or 'is' sometimes produce a different result?

I've got a Python program where two variables are set to the value `'public'`. In a conditional expression I have the comparison `var1 is var2` which fails, but if I change it to `var1 == var2` it... more

06/08/19

Calling an external command in Python?

How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
Python Xml

06/06/19

How do I parse XML in Python?

I have many rows in a database that contains xml and I'm trying to write a Python script that will go through those rows and count how many instances of a particular node attribute show up. For... more
Python List

06/06/19

How can I reverse a list in Python?

How can I do the following in Python? array = [0, 10, 20, 40] for (i = array.length() - 1; i >= 0; i--)I need to have the elements of an array, but from the end to the beginning.

06/06/19

Iterating over dictionaries using 'for' loops?

I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print key, 'corresponds to', d[key]What I don't understand is the `key` portion. How does Python... more

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.