(python) How do I obtain specific entries from a dictionary (using keys) as I do with an array?

txsaw1

With an array x=['A','B','C'], I can obtain several elements from it by just stating the index: eg.print(x[0:2]) yields ['A','B'].

Now for a similar (ordered) dictionary x={1:'A', 2:'B', 3:'C'}, how would I obtain 'A' and 'B' in the same way, by referencing the keys 1 and 2? Trying a method similar to the array above gives me an error:

TypeError: unhashable type: 'slice'

Note that the key tied to the entries are important, so it won't help converting the dictionary into a list.

Also, I plan on doing this to a lot of entries (>100), so calling each individual one won't be useful. My real program will involve numbered keys starting from 100 and calling keys 200 to 300, for example.

DeepSpace

The way to retrieve a value from a dictionary is dict_name[key]:

print x[1], x[2]
>> 'A', 'B'

Note that if the key doesn't exist this will raise a KeyError. A way around it is to use get(key, default_value):

print x[9]
>> KeyError
print x.get(9, None)
>> None

You can use a for loop in order to check multiple keys:

for potential_key in range(10):
    print x[potential_key]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I make a dictionary from a Python counter with the right keys?

From Dev

How do I use numbers in a list as keys for my dictionary in Python?

From Dev

How do I use numbers in a list as keys for my dictionary in Python?

From Dev

How do I configure multiple dictionary keys in a yaml file in Python?

From Dev

How do I delete specific entries wherein there are duplicate entries

From Java

How do I get the key at a specific index from a Dictionary in Swift?

From Dev

How do I create a dictionary from a specific table field?

From Dev

How do I create a list as a dictionary entry for a specific key in Python?

From Dev

How do I represent dictionary of dictionary in Python

From Dev

Using SignalR, how do I send a dictionary (associative array) to Javascript?

From Dev

How do I obtain this specific screen layout for android?

From Dev

how do I import an array containing dictionary from a text file?

From Dev

How do i extract an array from this plist dictionary?

From Dev

how do I import an array containing dictionary from a text file?

From Dev

How do I get the array values from two arrays by using keys

From Dev

How do I get a query from an array of primary keys using codeigniter framework

From Dev

How do I put specific elements from an array into another array

From Dev

How do I join an array of keys with an array of values to a dictionary without a loop?

From Dev

How do I avoid overriding a key from a global dictionary in python?

From Dev

How do I find common items from Python dictionary values?

From Dev

How do I remove \n from my python dictionary?

From Dev

How do I generate an adjacency matrix of a graph from a dictionary in python?

From Dev

Python: How do I randomly select a value from a dictionary key?

From Dev

How do I remove a dictionary index from a list on Python?

From Dev

How do I generate an adjacency matrix of a graph from a dictionary in python?

From Dev

In Javascript, how do I obtain a reference to an element in an array?

From Dev

How do I split an array into smaller arrays using a specific character?

From Java

How do I return a new dictionary if the keys in one dictionary, match the keys in another dictionary?

From Dev

How do I return a new dictionary if the keys in one dictionary, match the keys in another dictionary?

Related Related

  1. 1

    How do I make a dictionary from a Python counter with the right keys?

  2. 2

    How do I use numbers in a list as keys for my dictionary in Python?

  3. 3

    How do I use numbers in a list as keys for my dictionary in Python?

  4. 4

    How do I configure multiple dictionary keys in a yaml file in Python?

  5. 5

    How do I delete specific entries wherein there are duplicate entries

  6. 6

    How do I get the key at a specific index from a Dictionary in Swift?

  7. 7

    How do I create a dictionary from a specific table field?

  8. 8

    How do I create a list as a dictionary entry for a specific key in Python?

  9. 9

    How do I represent dictionary of dictionary in Python

  10. 10

    Using SignalR, how do I send a dictionary (associative array) to Javascript?

  11. 11

    How do I obtain this specific screen layout for android?

  12. 12

    how do I import an array containing dictionary from a text file?

  13. 13

    How do i extract an array from this plist dictionary?

  14. 14

    how do I import an array containing dictionary from a text file?

  15. 15

    How do I get the array values from two arrays by using keys

  16. 16

    How do I get a query from an array of primary keys using codeigniter framework

  17. 17

    How do I put specific elements from an array into another array

  18. 18

    How do I join an array of keys with an array of values to a dictionary without a loop?

  19. 19

    How do I avoid overriding a key from a global dictionary in python?

  20. 20

    How do I find common items from Python dictionary values?

  21. 21

    How do I remove \n from my python dictionary?

  22. 22

    How do I generate an adjacency matrix of a graph from a dictionary in python?

  23. 23

    Python: How do I randomly select a value from a dictionary key?

  24. 24

    How do I remove a dictionary index from a list on Python?

  25. 25

    How do I generate an adjacency matrix of a graph from a dictionary in python?

  26. 26

    In Javascript, how do I obtain a reference to an element in an array?

  27. 27

    How do I split an array into smaller arrays using a specific character?

  28. 28

    How do I return a new dictionary if the keys in one dictionary, match the keys in another dictionary?

  29. 29

    How do I return a new dictionary if the keys in one dictionary, match the keys in another dictionary?

HotTag

Archive