Your problem is that you have key
and value
in quotes making them strings, i.e. you’re setting aKey
to contain the string "key"
and not the value of the variable key
. Also, you’re not clearing out the temp
list, so you’re adding to it each time, instead of just having two items in it.
To fix your code, try something like:
for key, value in dict.iteritems(): temp = [key,value] dictlist.append(temp)
You don’t need to copy the loop variables key
and value
into another variable before using them so I dropped them out. Similarly, you don’t need to use append to build up a list, you can just specify it between square brackets as shown above. And we could have done dictlist.append([key,value])
if we wanted to be as brief as possible.
Or just use dict.items()
as has been suggested.
Related Posts:
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- In Python, when to use a Dictionary, List or Set?
- How can I get list of values from dict?
- Map to List error: Series object not callable
- How to return dictionary keys as a list in Python?
- How can I create an array/list of dictionaries in python?
- convert csv file to list of dictionaries
- Create a list with initial capacity in Python
- How can I reverse a list in Python?
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Iterating over dictionaries using ‘for’ loops
- Understanding slice notation
- How do I sort a dictionary by value?
- How do I sort a dictionary by value?
- How to convert list to string [duplicate]
- How to overcome TypeError: unhashable type: ‘list’
- How does collections.defaultdict work?
- Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- TypeError: ‘float’ object is not subscriptable
- Is there a short contains function for lists?
- How do I merge two dictionaries in a single expression (taking union of dictionaries)?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- Check if something is (not) in a list in Python
- Python list of dictionaries search
- Updating a dictionary in python
- unhashable type: ‘dict’ Type Error [duplicate]
- Print a list in reverse order with range()?
- TypeError: ‘list’ object cannot be interpreted as an integer
- Why is it string.join(list) instead of list.join(string)?
- Finding median of list in Python
- Python List vs. Array – when to use?
- Finding and replacing elements in a list
- ValueError: max() arg is an empty sequence
- How can I remove a key from a Python dictionary?
- Append integer to beginning of list in Python
- List comprehension on a nested list?
- Union of two lists in Python
- Python: Array v. List
- Writing a list to a file with Python
- Understanding slice notation
- Add list to set?
- What’s the idiomatic syntax for prepending to a short python list?
- How to remove an element from a list by index
- Apply function to each element of a list
- Creating 2D dictionary in Python
- How to pretty print nested dictionaries?
- How to deep copy a list?
- How to check if a string is a substring of items in a list of strings?
- How to index into a dictionary?
- Add a new item to a dictionary in Python
- How can I add new keys to a dictionary?
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- How do I check if a list is empty?
- IndexError: list index out of range and python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- Pop index out of range
- Print list without brackets in a single row
- How to iterate through two lists in parallel?
- TypeError: string indices must be integers, not str // working with dict
- How to remove \n from a list element?
- How do I check if there are duplicates in a flat list?
- Comparing two dictionaries and checking how many (key, value) pairs are equal
- How to save a dictionary to a file?
- Append a tuple to a list – what’s the difference between two ways?
- Defining lists as global variables in Python
- Subtracting two lists in Python
- How to concatenate items in a list to a single string?
- Python 2: AttributeError: ‘list’ object has no attribute ‘strip’
- How do I print the key-value pairs of a dictionary in python
- How to make a set of lists
- Appending to a list gives ‘int’ object has no attribute ‘append’
- Python data structure sort list alphabetically
- Appending values to dictionary in Python
- TypeError: unsupported format string passed to list.__format__
- Python dictionary from an object’s fields
- numpy-equivalent of list.pop?
- How to create a numpy array of lists?
- Python Add to dictionary loop
- AttributeError: ‘str’ object has no attribute ‘items’
- Declaring a multi dimensional dictionary in python
- how to concatenate two dictionaries to create a new one in Python?
- python-How to solve KeyError: 2?
- How to search through dictionaries?
- Element-wise addition of 2 lists?
- How to split elements of a list?
- Find object in list that has attribute equal to some value (that meets any condition)
- How can I extract all values from a dictionary in Python?
- Iterating through list of list in Python
- Python cant convert ‘list’ object to str error [closed]
- Get difference between two lists
- Determine if 2 lists have the same elements, regardless of order? [duplicate]
- Building a list inside a list in python
- python dictionary error AttributeError: ‘list’ object has no attribute ‘keys’
- Convert list of dictionaries to a pandas DataFrame