I have list = [] and I am adding an element to it using self.list.append(‘test’) and I get this error – AttributeError: ‘function’ object has no attribute ‘append’ The other list that I have defined append just fine, any ideas? Answer It seems you have a function in your code that is shadowing Python’s built-in function named list.
Tag: python-2.7
Return True if array contains a 2 or a 3
I’m having trouble with this CodingBat problem: Given an int array length 2, return True if it contains a 2 or a 3. I’ve tried two different ways to solve this. Can anyone explain what I’m doing wrong? Answer Your first one doesn’t work because the for loop in Python isn’t the same as the for loop in other languages.
Merge xml files with nested elements without external libraries
I am trying to merge multiple XML files together using Python and no external libraries. The XML files have nested elements. Sample File 1: Sample File 2: What I Want: What I have tried: From this answer. What I Get: I hope you can see and understand my problem. I am looking for a proper solution, any guidance would be
Python inserting variable string as file name
I’m trying to create a file with a unique file name for every time my script runs. I am only intending to do this to every week or month. so I chose to use the date for the file name. is where I’m getting this error. it works if I use a static filename, is there an issue with the
How to check if a specific integer is in a list
I want to know how to make an if statement that executes a clause if a certain integer is in a list. All the other answers I’ve seen ask for a specific condition like prime numbers, duplicates, etc. and I could not glean the solution to my problem from the others. Answer You could simply use the in keyword. Like
How to get the index with the key in a dictionary?
I have the key of a python dictionary and I want to get the corresponding index in the dictionary. Suppose I have the following dictionary, Is there a combination of python functions so that I can get the index value of 1, given the key value ‘b’? I know it can be achieved with a loop or lambda (with a
Find a value within nested json dictionary in python
From the following json, in python, I’d like to extract the value “TEXT”. All the keys are constant except for unknown. Unknown could be any string like “a6784t66” or “hobvp*nfe”. The value of unknown is not known, only that it will be in that position in each json response. one line json How would you get the value of “Text”?
Overriding in Python
I want to be able to do the following I would like to get a parent function from a child class in a function that is overriding it. I am not sure how to do this. This is a little hard to explain, comment if you are having trouble understanding. Edit: Thanks for the answers everyone, I almost thought that
Generate list of numbers in specific format
I need to generate a list of numbers in a specific format. The format is I know how to generate a normal list of numbers using range So, is there any built-in way in python to generate a list of numbers in the specified format. Answer Python string formatting allows you to specify a precision: Precision (optional), given as a
ImportError: No module named mock
So I am trying to use unittest.mock to mock some of my methods in my unit tests. I do: But I am getting: I tried: It’s still not working. Answer unittest is a built-in module; mock is an external library (pre-3.3 betas, anyway). After installing mock via pip install, you import it not by using but Edit: mock has been