0

I'm currently iterating through a dictionary and creating a list of strings, each one composed of a key / value from the dictionary, using this:

listA.append(attributesA + " " + thisObjectA.attributes[attributesA])

This gives me something like this:

['model taurus', 'make ford', 'color white']

I need to be able to reference a particular string in the list by index number or possibly by the first few letters in the string. I think what I want to do is use an array instead of a list. Not only will I be referencing multiple strings, from multiple arrays, I'll need to update certain strings with new strings. Would it be better if I used numpy for this?

Could you please suggest the best way to use an array instead of a list for this?

5
  • I don't know why you think an array would be any better for this. Why do you not like the list? Commented Sep 17, 2018 at 21:38
  • (Heck, what was wrong with the dictionary?) Commented Sep 17, 2018 at 21:39
  • If you are bent on using array only, you can convert it later as np.asarray(listA). Just to answer your particular flavour Commented Sep 17, 2018 at 21:41
  • I'm creating the list from two dictionaries. So, I suppose I could have created a new dictionary instead. My main objective is comparing strings (or key/values) from two lists and they'll be in the same index position. If I used dictionary instead of a list, I think i'd need to reference a key/value by key and not position? Commented Sep 17, 2018 at 21:59
  • If you have two dictionaries you could just compare the keys or values of what you are looking for and eliminate this entire process, dictionaries are very useful, maybe post a question on how to get the results you desire using the dictionaries you already have created Commented Sep 17, 2018 at 22:05

1 Answer 1

1

This method seems fine to me if you are having trouble identifying the index for each one you can use enumerate your lists and return the two indexes for the item you are locating.

Although I would recommend staying with the dictionaries, which most likely should be able to accomplish your task

lista = ['model taurus', 'make ford', 'color white']

for index, item in enumerate(lista):
    print(f"{item}: {index}")
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 yodish.py 
model taurus: 0
make ford: 1
color white: 2
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, I can identify the index of a small list like this by eyeballing it. I'd like to be able to retrieve a string by index number, then retrieve a string of another list (by index number), do some logic and possibly update a string (via index number). In the above example: Get [2], do something, replace [2] with 'make chevy'
As mentioned maybe try a new question with your data and what you want to accomplish I'm sure we will be able to find a solution :)
but you could use enumerate and do a search if item in list is something return index and then use those two indexes from each list
I think that's basically what i'm looking to do. I wasn't sure if I had to enumerate or if I could go directly to an index position in a list to retrieve something; that's why I brought numpy into the question.
yeah set up a for loop that does something like if item == 'ford', x = index and then you cand apply that index to your list
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.