0
searchindexnodes = []
searchindexnodes[1036592] = 'Apparel'
searchindexnodes[672123011] = 'Apparel'
searchindexnodes[165796011] = 'Baby'

This doesn't appear to be working. Any thoughts?

2 Answers 2

5

I think a better solution would be to use a dictionary. If you wanted a list with empty elements you'd be creating a list with over 100,000,000 elements which would be a huge waste of memory.

searchindexnodes = {}
searchindexnodes[1036592] = 'Apparel'

Python implements searching quickly in dictionary data structures. You could check if an element is present by doing something like

if 1036592 in searchindexnodes:
    print "It's there!"

Edit to iterate through the whole list you can do something like if you want the key and value

for key, value in searchindexnodes.items():
    print "{0} --> {1}".format(key,value)

otherwise what's below will loop through each key

for key in serchindexnodes:
    print key
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! How can I iterate over searchindexnodes after?
@Shamoon: as any other iterable object: for i in searchindexnodes:
@Shamoon: See items and iteritems in the docs: docs.python.org/library/stdtypes.html#dict.items
3

use a dictionary for that:

searchindexnodes = {}
searchindexnodes[1036592] = 'Apparel'
searchindexnodes[672123011] = 'Apparel'
searchindexnodes[165796011] = 'Baby'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.