-6
      char_rdic = list('helo')
      char_dic = {w:i for i ,w in enumerate(char_rdic)}

I'm not really sure what this code actually do. what does w and i represent in this code?

2 Answers2

1

This is a dict comprehension. If you are familiar with list comprehension ([do_stuff(a) for a in iterable] construct), that works very much the same way, but it builds a dict.

See Create a dictionary with list comprehension in Python and https://docs.python.org/3.5/tutorial/datastructures.html#dictionaries for the official documentation

Guillaume
  • 5,497
  • 3
  • 24
  • 42
  • thank you for comment. I do not understand people who keep giving negative things. my native language is not english. and i'm new to programming and also 12 years old. i'm trying to learn. but people are so mean. –  Sep 12 '16 at 01:35
0

This code creates char_dic dictionary with chars from 'helo' string as keys and their occurence indexes in given string. i is an index of w element in char_rdic list.

Abdulafaja
  • 262
  • 2
  • 8