Home > Back-end >  How to assign the value of an index of a list to a variable in Python if you only know the index num
How to assign the value of an index of a list to a variable in Python if you only know the index num

Time:07-18

I just spent 4 hours trying to google the answer asked pretty much as above. There were 10,000 results on how to find the index of an element. But I am not interested in any of the elements and never know what they will be, how long they will be, nor what they will contain since the string is user's input.

I have studied the Python manual for "lists" and tried several methods. I used loops of several types. List comprehensions were simply too complicated for my tiny brain although I tried. In fact, I tried for 4 hours googling and tweaking things maybe a hundred times but I only received errors after error of every type which I chased down one by one. I am not providing any code because I am not interested in having anyone fix it (its for an online course and that would be cheating). I just want to know the syntax for one little thing that is preventing me from making it work.

I need to assign the position of any element (i.e., the index integer value) anywhere in the list to a variable so I can control some boundaries. The most important thing for me is to set some conditions for the first character of the string converted to a list.

"if this is index 0 of the list then do this"

If I know how to do that then I can extrapolate it for other character positions in the list.

I am an total beginner, and am not familiar with technical jargon, so please provide the simplest, least complex method! :-) Thank you in advance.

CodePudding user response:

I also tried this one: I think this is what you want. It assigns the value of an index (0,1,2,3) of a list to a variable.

 list = ["item 1", "item 2", "item 3"]
 item = input("Enter some text: ")
 if item == list[0]:
     index = 0
 elif item == list[1]:
     index = 1
 elif item == list[2]:
     index = 2
 print(index)

CodePudding user response:

I tried this because you said: "if this is index 0 of the list then do this"

It checks if the value of the input is the item with index 0 of the list.

list = ["item 1", "item 2", "item 3"]
item = input("Enter some text: ")
if item == list[0]:
   print("This is index 0 of the list")
else:
   print("This is not index 0 of the list")

If this is not the thing you're looking for, could you please try to explain it in a different way? Or maybe try writting some code too please.

CodePudding user response:

I'm an amateur myself but I will take a shot. If you can please do share some more context and some code for clarity.

I just checked the comment you made 50 mins ago. If I understand correctly, you want to assign the indexes to a variable. If that's correct can use the enumerate function. It's a built-in function for sequences. If we were to apply enumerate on our list named text it will return the position e.g. the index and the value of that position.

text = ["B", "O", "O", "M"]
for index, value in enumerate(text):
    print(index, value)

This code will give you the following result:

0 B
1 O
2 O
3 M

Inside the for loop, you have the index variable that will now refer to the position of each value. You can now apply further conditions, like if index == 0:... and do your thing.

Does this help?

  • Related