Help me python3(solved) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Help me python3(solved)

Can someone help me, i want to make the alphabet enter in dictionary instead of put in one by one,whats wrong in this code? https://code.sololearn.com/c9iD0fJhj105/?ref=app

10th Aug 2018, 2:13 PM
I Am Arthur
I Am Arthur - avatar
12 Answers
10th Aug 2018, 3:09 PM
Davod
Davod - avatar
+ 6
As already mentioned, a dictionary has keys and values and looks like this: alphabet = { 'first_letter': 'a', 'second_letter': 'b', ... } This is a list: alphabet = ['a', 'b', 'c', ...] To convert a string into a list in python, you have to do - nothing! In python, a "string" is a list of characters per se. That's why you can do things like string[5] to access the 6th character of the string or for letter in string: print(letter) to iterate over all letters/characters of the string. So, x = 'abcdef...' already is a list! You don't need lines 3 to 5. The only potential issue here is that this kind of list is immutable, so you can't use x[3] = 'm' to assign a new value to the 4th list entry. But I think you don't want to do this here anyway... P.S. Better don't parenthesize your variables like in line 1. In some situations, they might be (mis)interpreted as tuples or some other data type you don't want
11th Aug 2018, 8:18 AM
Anna
Anna - avatar
+ 4
You need no loop, you can just write: xdic.extend(x)
10th Aug 2018, 2:47 PM
HonFu
HonFu - avatar
+ 3
alph={i:chr(i+96) for i in range(1,27)} print(alph)
10th Aug 2018, 8:07 PM
Louis
Louis - avatar
+ 1
Well, I can not understand what exactly your code is doing with range function but: xdic= [] - declares an empty list, not dictionary. that is why you chould add an item with append method, since your list is empty: xdic.append(i)
10th Aug 2018, 2:22 PM
strawdog
strawdog - avatar
+ 1
strawdog Yeah, i always change the names (dictionary and list)
10th Aug 2018, 2:31 PM
I Am Arthur
I Am Arthur - avatar
+ 1
@Arthur 1. Append method perfectly works with strings. 2. You can not assign a new value to a list by index if there is no any value with such index. That is why i told you you declared an _empty_ list. And that is why youy need to append/extend the list with new value. As far as I understand, you just want to have a list of all alphabet letters. It could be easily obtained with this code: >>> x = "abcdefghijklmnopqrstuvwxyz" >>> my_alpha_list=list(x) >>> print(my_alpha_list) ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
10th Aug 2018, 3:02 PM
strawdog
strawdog - avatar
+ 1
Louis i arealdy solved..
10th Aug 2018, 8:47 PM
I Am Arthur
I Am Arthur - avatar
+ 1
takes two iterables and joins them , typically the result is tuples, but here I generate a dictionary by a range and str and zip the two together
12th Aug 2018, 4:03 AM
Davod
Davod - avatar
0
ord(i) - ord('a')+1 # no magic number 😄 post taking on life of it's own?
10th Aug 2018, 9:05 PM
Davod
Davod - avatar
0
Davod what does 'zip(s,y)' do in your code?
11th Aug 2018, 1:53 PM
Pro_me.py
Pro_me.py - avatar
0
Pro_me.py It's like a zipper😂
12th Aug 2018, 4:01 AM
Davod
Davod - avatar