Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,961 questions

51,903 answers

573 users

How to search a list of dictionaries in Python

4 Answers

0 votes
list_of_dictionaries = [{"lang": "java", "jobs": 5},
                        {"lang": "php", "jobs": 7},
                        {"lang": "python", "jobs": 4},
                        {"lang": "c++", "jobs": 2}]

result = (item for item in list_of_dictionaries if item["lang"] == "python").__next__()

print(result)


'''
run:

{'lang': 'python', 'jobs': 4}

'''

 



answered Nov 10, 2017 by avibootz
edited Nov 10, 2017 by avibootz
0 votes
list_of_dictionaries = [{"lang": "java", "jobs": 5},
                        {"lang": "php", "jobs": 7},
                        {"lang": "python", "jobs": 4},
                        {"lang": "c++", "jobs": 2}]

result = filter(lambda item: item['lang'] == 'python', list_of_dictionaries).__next__()

print(result)


'''
run:

{'lang': 'python', 'jobs': 4}

'''

 



answered Nov 10, 2017 by avibootz
edited Nov 10, 2017 by avibootz
0 votes
list_of_dictionaries = [{"lang": "java", "jobs": 5},
                        {"lang": "php", "jobs": 7},
                        {"lang": "python", "jobs": 4},
                        {"lang": "c++", "jobs": 2}]

result = [element for element in list_of_dictionaries if element['lang'] == 'python']

print(result)


'''
run:

[{'lang': 'python', 'jobs': 4}]

'''

 



answered Nov 10, 2017 by avibootz
edited Nov 10, 2017 by avibootz
0 votes
list_of_dictionaries = [{"lang": "java", "jobs": 5},
                        {"lang": "php", "jobs": 7},
                        {"lang": "python", "jobs": 4},
                        {"lang": "c++", "jobs": 2}]

result = next(item for item in list_of_dictionaries if item["lang"] == "python")

print(result)


'''
run:

{'lang': 'python', 'jobs': 4}

'''

 



answered Nov 10, 2017 by avibootz

Related questions

1 answer 83 views
1 answer 151 views
1 answer 204 views
1 answer 190 views
1 answer 175 views
1 answer 146 views
1 answer 120 views
...