+ 1

in a loop for, when running through a list, the value that is obtained what data type is it? or what data type does it belong to

Abc=["a","b","c"] ca={"a":1,"b":2,"c":3} for x in Abc: print(ca[x]) I have tried to convert x to a string but it doesn't work

19th Apr 2022, 9:57 AM
Amazon
Amazon - avatar
4 Respuestas
+ 1
It is the same data type as in Abc, hence string. So you don't need to convert it. Which output do you expect?
19th Apr 2022, 10:18 AM
Lisa
Lisa - avatar
0
x is already a string taken from Abc list when you write ca[x] so it is same as writing ca["a"] ca["b"] ca["c"]
19th Apr 2022, 10:18 AM
NonStop CODING
NonStop CODING - avatar
0
But it is already string x is the same data in the list so you print ca["a"] which will output 1.
19th Apr 2022, 4:12 PM
Mohamed Ayaou
0
Mohamed Ayaou yeah ca["a"] will be 1 x = "a" in python for loop we iterate through each element of list, we don't need list index to do that
19th Apr 2022, 4:17 PM
NonStop CODING
NonStop CODING - avatar