0

I must find a way to get the item position when iterating over a list using a for structure

other_list = ["line1", "line2", "line3", ... , "line125k+"]
#contains 125k+ items from a txtFile.readlines()

list = ["item1", "item2", "item3"]
#contains 35 items

dict = {"key1":["value1"], "key2":["value2"], "key3":["value3"]}
#contains 35 items too

For each value inside my dict, i got a key that have a correspondent item in the list.

list = ["10", "20", "30"]`
dict = {"19":["value1"], "29":["value2"], "39":["value3"]}

the dict's first key, "19", corresponds to the "10" inside the other list..

Example:
dict[0] corresponds to list[0]
dict[1] corresponds to list[1]
... and so on.

So i have to get the item position while using a for structure, so then i can access the correspondent key in the dict, and use the dict's value in a replace()

#replace tax1 value
for item in list:
    pos_item = item.getPosition() # pos_item = getIteratorValue()
    #how can i assign the dict value to a variable?
    dict_value = dict[pos_item][value]
    #use one variable to search and the other as a replacement
    other_list[pos_item].replace("0,00", dict_value)
10
  • I think it's not possible to get the index in a foreach loop because of the iterator model. The only thing you could do is set a variable to 0 before the loop and increment it every single run... Commented Jul 18, 2015 at 20:22
  • 6
    dicts have no order so your logic if flawed Commented Jul 18, 2015 at 20:22
  • 1
    Since dictionaries are not ordered you can not compare its item's position with a list elements! Commented Jul 18, 2015 at 20:23
  • 1
    Based on what you want to do you have 2 choice, at first use a OrderdDict instead of a dict or sort your dict items and work with the sorted result which is not a dictionary anymore and is a list! Commented Jul 18, 2015 at 20:28
  • 3
    You could use enumerate(), to find out the current position in the iteration . Obviously, using index won't allow to get dict's items Commented Jul 18, 2015 at 20:33

2 Answers 2

1

There are three issues here, not one. In Python (and most other languages), dictionaries aren't sorted, and replace returns a new string. They have no order. In order to get around this, you can use an OrderedDict and do something like:

# The dictionary.
dct = OrderedDict([('key1', 'value1'), ('key2', 'value2')]
for pos_item, (item, dict_values) in enumerate(zip(lst, dct.values())):
   dict_value = dict_values[value]
   other_list[pos_item] = other_list[pos_item].replace('0,00', dict_value)

Look into enumerate and zip.

Also notice that I renamed dict to dct and list to lst, as dict and list builtin functions. In addition, there may be a bug in your code, as you are never actually using item; I'm not sure what it's purpose in your code was.

Sign up to request clarification or add additional context in comments.

Comments

0

This should be helpfull:

list = ["item1", "item2", "item3"]

for i in xrange(len(list)): # len(list) returns length of the list
    print(list[i])

2 Comments

xrange is Python 2-only.
There is a python-2.7 tag in this question ;-)

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.