dict « List « 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 » List » dict 

1. Do Python lists have an equivalent to dict.get?    stackoverflow.com

I have a list of integers. I want to know whether the number 13 appears in it and, if so, where. Do I have to search the list twice, ...

2. Converting tree list to hierarchy dict    stackoverflow.com

I have a list of elements with attrs: parent, level, is_leaf_node, is_root_node, is_child_node. I want to convert this list to hierarchy dict. Example of output dict:

{
       ...

3. can Bisect be applied using Dict instead of lists?    stackoverflow.com

i have a file with information in the pattern of:

.343423 1
.434322 1
.453434 1
.534342 1
Equal size each line and row in sorted order..I have a variable "a" with a value and need ...

4. Keyword argument in unpacking argument list/dict cases in Python    stackoverflow.com

For python, I could use unpacking arguments as follows.

def hello(x, *y, **z):
    print 'x', x
    print 'y', y
    print 'z', z

hello(1, *[1,2,3], ...

5. In Python, how to add distinct items to a list of dicts from another dict?    stackoverflow.com

In Python, the original dict list is as follows:

orig = [{'team': 'team1', 'other': 'blah', 'abbrev': 't1'},
        {'team': 'team2', 'other': 'blah', 'abbrev': 't2'},
   ...

6. Create ranked dict with list comprehension    stackoverflow.com

I have a list [5, 90, 23, 12, 34, 89] etc where every two values should be a (ranked) list in the dictionary. So the list above would become {1: [5, 90], ...

7. How can you convert a list of poorly formed data pairs into a dict?    stackoverflow.com

I'd like to take a string containing tuples that are not well separated and convert them into a dictionary. s = "banana 4 apple 2 orange 4" d = {'banana':'4', 'apple':'2', 'orange':'4'} I'm running ...

8. Python Optimized Comparison Between List of Dict    stackoverflow.com

I'm trying to see whether nodes reside within the volume of a sphere, and add the node id to a list. However, the efficiency of the algorithm is incredibly slow and ...

9. how to convert list of dict to dict    stackoverflow.com

How to convert list of dict to dict. Below is the list of dict

data = [{'name': 'John Doe', 'age': 37, 'sex': 'M'},
        {'name': ...

10. List comprehension on Python dict doesn't perform well    stackoverflow.com

I have the following loop:

for row in rows:
    print row.value('customer', 'name')
#   print [customer for customer in customers if customer['name'] == row.value('customer', 'name')]
When I uncomment the second ...

11. python list/dict property best practice    stackoverflow.com

I have a class object that stores some properties that are lists of other objects. Each of the items in the list has an identifier that can be accessed with the ...

12. Converting a list of dict to a list of a specific value    stackoverflow.com

I'd like to know the most elegant (and performant?) pattern to replace what I often code as:

names = []
for person in persons:
    names.append(person.GetName())
I write this because it's how ...

13. [] and {} vs list() and dict(), which is better?    stackoverflow.com

I understand that they are both essentially the same thing, but in terms of style, which is the better (more Pythonic) one to use to create an empty list or dict? ...

14. SyntaxError: invalid syntax while building a dict from a list using Python 2.6    stackoverflow.com

I created a Python script with Python2.7 and it works fine. However, when I run the same script with Python2.6, I got a "SyntaxError: invalid syntax" error. After investigating, the problem seems ...

15. how to uniqify a list of dict in python    stackoverflow.com

I have a list:

d = [{'x':1, 'y':2}, {'x':3, 'y':4}, {'x':1, 'y':2}]
{'x':1, 'y':2} comes more than once I want to remove it from the list.My result should be:
 d = [{'x':1, 'y':2}, ...

16. Get all values from child elements of a dict as a list    stackoverflow.com

T = {'a': {'c': 'A', 'path': '/c'}, 'e': {'c': 'E', 'path': '/e'}, 's': {'c': 'S', 'path': '/s'}}
I need all 'path' elements as a list. I know I can iterate through everything ...

17. python getting a list of value from list of dict    stackoverflow.com

I have a list of dict like this: [{'value': 'apple', 'blah': 2}, {'value': 'banana', 'blah': 3} , {'value': 'cars', 'blah':4}] I want ['apple', 'banana', 'cars'] Whats the best way to do this?

18. Accept either single dict or list of dicts as function argument    stackoverflow.com

I'd like to accept either one dict or a list of dicts as a function argument. So far, I've come up with the following, but I suspect I've missed something completely ...

19. Pickle and Unpickle dict w/list as value    stackoverflow.com

I have a dictionary with a list as a value such as:

numlist = {'Person': ['2342342', '15:05']}
I pickle it.
outfile = open{"log.txt", "wb")
pickle.dump(numlist, outfile)
I unpickle it.
infile = open("log.txt", "rb")
pickle.load(infile)
How do I convert this ...

20. Which way to append dict to list is more efficient    stackoverflow.com

# Method one
array_a = []
a = {}
for i in range(5):
    a = {}
    a[str(i)] = i
    array_a.append(a)
print(array_a)
# [{'0': 0}, {'1': 1}, {'2': ...

21. Python: simple iteration of lists in a dict    stackoverflow.com

I have this simple structure:

o = {
    2: [0, 148, 149, 150, 151],
    3: [0, 152, 153, 154, 155, 156],
    4: [0, ...

22. Fastest way to convert sql result into a dict or list ?    bytes.com

Hello, I'm trying to find the fastest way to convert an sql result into a dict or list. What i mean, for example: my sql result: contact_id, field_id, field_name, value sql_result=[[1, 1, 'address', 'something street'], [1, 2, 'telnumber', '1111111111'], [1, 3, 'email', 'something@something.net'], [2, 1, 'address','something stree'], [2, 3, 'email','something@something.net']] the dict can be: dict={1:['something street', '1111111111' , 'something@something.net'], 2:['something street', ...

23. remove a element from list of dict.    python-forum.org

Hi, I am new to python, i want to delete the any elemet from a dict of list. Say from the below example, i want to delete 'sona'. How can i achive this? dict_list = {'name': ['abhi', 'pankaj', 'sona', 'kajol']} Actually i am running a loop, where if i found any bad value in the above list then i want to ...

24. No duplicated lists in dict Value    python-forum.org

Hi evryone, I'm learning Python and I work on a little script at the moment. The script reads a TAB separated file with basically the following content format. ------------------- 1101,103,105,231 2102,203,234 1101,104 2102,400,236,345 ------------------- The script itterates over all the lines and, splits the line and creates two variables. (id,value) = line.split("/t") Then the script creates a dict where, id is ...

25. Comparing Elements in a List of Dict    python-forum.org

Read this and you will understand why. Code: Select all In [1]: for i in xrange(1,10): ...: for e in xrange(1,20): ...: print i, e ...: 1 1 1 2 1 3 1 4 1 5 1 6 1 7 ...

26. logical dict and list problem    python-forum.org

so I have a dictionary of user names and apps It goes like this: {"name1":["app1","app3"],"name2":["app2","app3","app5"].......} It runs my whole site by deciding what users have access to which apps. There are about ten to thirteen users in total. The problem is my javascript app list editor is returning a list like this: (["name1","app1"],["name1","app3"],["name2","app2"],["name2","app3"],["name2","app5"]...) I've been fighting with it for a few ...

27. convert text file to list, to dict, play with its words    python-forum.org

Hi, I know this problem is pretty stupid, i got something like 4 kind of error trying to write this piece of code (Think python exercise).. But I'm still hopeful I got a typeerror Nonetype object is not iterable, Then an index error and then i probably wrote too many for loops (and chaging them into while loops wasn't helpful ...) ...

28. Dict in list problem    python-forum.org

29. a dict out of two lists:    python-forum.org

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.