0

Lets assume that we have a list of elements which looks like this

identifier, url
cars, /cars/1234
motorcycles, /motorcycles/jd723
yachts, /yachts/324lkaj
trucks, /trucks/djfhe

The idea behind this, that i have a static list of items, which will nearly not change. I could put them into an JSON or even a Database, but this would 'cost' time, in my opinion.

Therefore i tought about having this array/list statically in Python.

What i a would need is, how to search for yachts and get back the second item of the list. What i found so far on the net ist always based on a 2 or multi-dimensional array and then pick the n-th item out of.

How would you do this in Python.

STORM
  • 4,005
  • 11
  • 49
  • 98
  • See https://docs.python.org/3/tutorial/datastructures.html#dictionaries – Cireo Dec 15 '22 at 17:27
  • Shame on me. Yes, dictionary is the magic word. Please post this and i will accept it as an answer. – STORM Dec 15 '22 at 17:29
  • No worries, we'll probably just redirect this directly to some generic dictionary question =D – Cireo Dec 15 '22 at 17:31

1 Answers1

0

You should create a dictionary and access the element you need using the associated key.
This is an example:

dictionary = {"cars": "/cars/1234", "motorcycles": "/motorcycles/jd723", "yachts": "/yachts/324lkaj", "trucks": "/trucks/djfhe"}
print(dictionary["yachts"])
Diego
  • 68
  • 1
  • 4