item « dictionary « Python Data Type Q&A

Home
Python Data Type Q&A
1.array
2.date
3.dictionary
4.Format
5.integer
6.List
7.numpy
8.regex
9.string
10.tuple
Python Data Type Q&A » dictionary » item 

1. In Python, how can you easily retrieve sorted items from a dictionary?    stackoverflow.com

Dictionaries unlike lists are not ordered (and do not have the 'sort' attribute). Therefore, you can not rely on getting the items in the same order when first added. What is ...

2. How do you retrieve items from a dictionary in the order that they're inserted?    stackoverflow.com

Is it possible to retrieve items from a Python dictionary in the order that they were inserted?

3. Getting a list of specific index items from a list of dictionaries in python (list comprehension)    stackoverflow.com

I have a list of dictionaries like so:

listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}]
I want a list of all the ids from the dictionaries. So, from the given list I would get the list:
[1,3,5]
It ...

4. item frequency in a python list of dictionaries    stackoverflow.com

Ok, so I have a list of dicts:

[{'name': 'johnny', 'surname': 'smith', 'age': 53},
 {'name': 'johnny', 'surname': 'ryan', 'age': 13},
 {'name': 'jakob', 'surname': 'smith', 'age': 27},
 {'name': 'aaron', 'surname': 'specter', 'age': 22},
 ...

5. In Python, find item in list of dicts, using bisect    stackoverflow.com

I have a list of dicts, something like this:

test_data = [
    { 'offset':0, 'data':1500 },
    { 'offset':1270, 'data':120 },
    { 'offset':2117, 'data':30 ...

6. concatenate items in dictionary in python using list comprehension    stackoverflow.com

EDIT: Clarified the question a bit How can I get a string from a dictionary with the format

key1 = value1
key2 = value2
in a relatively fast way ? (relative to plain concatenation) ...

7. Using a list objects to look up items in a dictionary in Python    stackoverflow.com

I have a a wxPython checklist box that returns a list of integers. I want to use the integers to look up items in a dictionary. I am not really sure ...

8. Python XML to dictionary to iterate over items    stackoverflow.com

I have the following XML example

<?xml version="1.0"?>
<test>
    <items>
        <item>item 1</item>
        <item>item 2</item>
   ...

9. How many items in a dictionary share the same value in Python    stackoverflow.com

Is there a way to see how many items in a dictionary share the same value in Python? Let's say that I have a dictionary like:

{"a": 600, "b": 75, "c": 75, "d": ...

10. How do I get the key of an item when doing a FOR loop through a dictionary or list in Python?    stackoverflow.com

I am new to Python. Say I have a list:

list = ['A','B','C','D']
The key for each item respectively here is 0,1,2,3 - right? Now I am going to loop through it with a for ...

11. How do I concatenate strings from a dictionary by identifying the last item with Python?    stackoverflow.com

I need to concatenate string to an existing one as follows.

for k,v in r.iteritems():
    tableGenString += "%s %s, " % (k, what_type(v))
The problem is that for the last ...

12. Python decorator class to transform methods to dictionary items    stackoverflow.com

I wonder if there is a reasonable easy way to allow for this code (with minor modifications) to work.

class Info(object):
    @attr("Version")
    def version(self):
   ...

13. Why doesn't mydict.items().sort() work?    stackoverflow.com

When I try and sort my dictionary, I get an error: ''nonetype' object is not iterable. I am doing:

for k,v in mydict.items().sort():

14. add item to dictionary    stackoverflow.com

i read a lot in that forum, but i couldn't find a proper way to add all items to my dictionary... So maybe someone can help me! first a explanation:

rows = cur.fetchall() ...

15. Python: Using a dictionary to count the items in a list    stackoverflow.com

I'm new to Python and I have a simple question, say I have a list of items:

['apple','red','apple','red','red','pear']
Whats the simpliest way to add the list items to a dictionary and ...

16. iterating over dictionary items(), values(), keys() in p3k    stackoverflow.com

If I understand correctly, in Python 2, iter(d.keys()) was the same as d.iterkeys(). But now, d.keys() is a view, which is in between the list and the iterator. What's the difference ...

17. Finding maximum value in a dictionary containing mixed items in Python    stackoverflow.com

I have a dictionary with either a integer or a tuple of integers as value. How do I find the maximum integer present in dicts' values? Example:

x1 = {0:2, 2:1, 3:(1, 2), ...

18. Python reclaiming memory after deleting items in a dictionary    stackoverflow.com

I have a relatively large dictionary in Python and would like to be able to not only delete items from it, but actually reclaim the memory back from these deletions in ...

19. My dictionary has 9 items yet when iterating with for there are only eight    stackoverflow.com

Here is my code:

trainingSet = {"0,0":0, "1,0":1, "2,0":1, "0,1":0, "1,1":1, "1,2":0, "2,0":0, "2,1":0, "2,2":1}

for k,expectedOutput in trainingSet.iteritems():
    print 1
    coordinates = k.split(",")
    ...

20. Cannot add new items into python dictionary    stackoverflow.com

Hi I'm new to python. I am trying to add different key value pairs to a dictionary depending on different if statements like the following:

def getContent(file)
    for line ...

21. Python sort items by specific defined rule    stackoverflow.com

say i have a dict: d = {'Abc':5,'Jack':4,'amy':9,'Tom':0,'abc':5} If i want to write a function such that if i pass that function to the built-in sort function, eg. list(d).sort(function), the sort function ...

22. counting a sequence of item, python    stackoverflow.com

The task is to define a function count_vowels(text) that takes a string text, counts the vowels in text (using a Python dictionary for the counting), and returns the vowel frequency information as a ...

23. aggregate dictionary items into summary results    stackoverflow.com

I have a dictionary like

    mydict={
        (a,1):0,
        (a,2):0,
      ...

24. Python: Get index of dictionary item in list    stackoverflow.com

I have a list li:

[
{name: "Tom", age: 10},
{name: "Mark", age: 5},
{name: "Pam", age: 7}
]
I want to get the index of the item that has a certain name. For example, if I ...

25. Creating a dictionary of 700k items in Python?    stackoverflow.com

I have a class called Point which has only x_coordinate and y_coordinate floats as its attributes. I have about 700000 of those Point objects which I would like to store in ...

26. deleting items from a dictionary while iterating over it    stackoverflow.com

Is it legitimate to delete items from a dictionary in Python while iterating over it? For example:

for k, v in mydict.iteritems():
   if k == val:
     del ...

27. Python: get all values associated with key in a dictionary, where the values may be a list or a single item    stackoverflow.com

I'm looking to get all values associated with a key in a dictionary. Sometimes the key holds a single dictionary, sometimes a list of dictionaries.

a = {
  'shelf':{
   ...

28. Get the name of a list item    stackoverflow.com

I have easy access to a list of variables as such:

[a, b, c,]
I was wondering, can you introspect (or something) on the variable names to get a dict that looks like ...

29. Shortcut to print only 1 item from list of dictionaries    stackoverflow.com

l = [
    {'bob':'hello','jim':'thanks'},
    {'bob':'world','jim':'for'},
    {'bob':'hey','jim':'the'},
    {'bob':'mundo','jim':'help'}
]

for dict in l:
    print dict['jim']
Is there a one liner ...

30. "TypeError: 'type' object does not support item assignment" when trying to use dictionaries    stackoverflow.com

This is a very basic snippet adder/viewer/remover. The way I want it to work, the user would enter a name (dictionary key) and then the text body (value of key), to ...

31. Perform actions on dictionary values that match items in a list?    stackoverflow.com

I have a dictionary with 46,000 key:value pairs where each key has a 3 item list as values:

my dict = {key1: ['A', 'B', 'C'], key2: ['B', 'A', 'G'], key3: ['Z', 'H', ...

32. How to know the position of items in a Python's ordered dictionary     stackoverflow.com

Can we know the position of items in Python's ordered dictionary ? For example: If I have dictionary :

//  Ordered_dict is OrderedDictionary

 Ordered_dict = {"fruit": "banana", "drinks": "water", "animal": "cat"}
Now how to ...

33. How to manage a python dictionary with three items and add the last one?    stackoverflow.com

I have a dictionary in python like this: x = {country:{city:population}:......} and I want to create a new dictionary like y = {country:cities_population}, where cities_population adds all the population in every city in ...

34. what is dict_keys, dict_items and dict_values    stackoverflow.com

I came across these three types when I used collections.Counter's viewkeys(),viewitems() and viewvalues() method. The value those three methods returned are of type dict_keys, dict_items and dict_values. They are iterable, as I have ...

35. extract item from list of dictionaries    stackoverflow.com

Suppose you have a list of dictionaries like this one:

a = [ {'name':'pippo', 'age':'5'} , {'name':'pluto', 'age':'7'} ]
What do you to extract from this list only the dict where name==pluto? To make ...

36. Quick and easy way to check if all items in a dictionary are empty strings?    stackoverflow.com

I have a dictionary of N items. Their values are strings, but I'm looking for an easy way to detect if they are all empty strings.

{'a': u'', 'b': u'', 'c': u''}
...

37. How to retrieve a single item from nested dictionary in python    stackoverflow.com

I am reading from a file with data like this:

{"day" :"Monday", "alarm":"on", "kids":"School" , "work":"days"}
{"day" :"Tuesday", "alarm":"on", "kids":"School" , "work":"days"}
{"day" :"Wednesday", "alarm":"on", "kids":"School" , "work":"days"}
{"day" :"Thursday", "alarm":"on", "kids":"School" , "work":"nights"}
{"day" :"Friday", ...

38. What is the proper way of deleting item from a dictionary in "for" loop (Python)?    stackoverflow.com

I want to iterate a dictionary, examine the value and delete items that matches certain values. Example

d = {1, 1, 2, 1, 4, 5}   
for i in d:
  ...

41. Trying to exclude items from a dictionary with an if stateme    python-forum.org

Trying to exclude items from a dictionary with an if stateme by stevo Thu Apr 22, 2010 3:41 pm After reading a few books over past few days I decided to redo one of my original projects, with my vastly improved understanding of dictionaries. Cleaned up the code a bit, did some debugging print statements to verify the dictionaries were ...

42. How "point" to an item in a dictionary using a list    python-forum.org

>>> dict = { ... 'a' : 0, ... 'b' : { ... 'c' : 1, ... 'd' : { 'e' : 7 } ... } ... } >>> dict {'a': 0, 'b': {'c': 1, 'd': {'e': 7}}} >>> location1 = ['b','d','e'] >>> dic = dict >>> ...

43. Understanding the .items method for dictionaries?    python-forum.org

Your understanding of the .items method for dictionaries is spot on. However, never ever, ever never name a variable the same name as a python builtin type. dict is a builtin python type. Once you overwrite a builtin in type or a builtin function by assigning to that name, strange things will happen. Because you are just beginning python and you ...

44. List of Dictionary Items Problem    python-forum.org

def add_rec(from_value, subject_value): test = {} test['From'] = from_value test['Subject'] = subject_value return test ##----------------------------------------------------- list_test =[] data_list = [ ['modtodd@wi.rr.com', 'Test Subject'], ['hanson@wi.rr.com', 'Test Subject 2'], ['thanson@qisadvisors.com', 'Test Subject 3'] ] for rec in data_list: ...

45. Order of items extracted from a dictionary?    python-forum.org

It all goes back to the basics of the Python language design: List items have a consecutive index, and are accessed by index. Dictionary items are accessed by their key, so the index of the key doesn't matter. Actually, finding a key in a dictionary is faster than finding an item in a list.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.