Python Forum
Feed List items with Integer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Feed List items with Integer
#1
I try to get a list item by feeding it with Int variable, but cannot find a way to do it.
# this one works
cars = ["Audi", "Opel", "BMW"]
for x,y in enumerate(cars):
    temp = int(x)
    print(cars[temp])
But when I try to feed an item directly with a variable, I get an error. I want to use direct method in WebAutomation (looping and picking elements in HTML).
# this one not
cars = ["Audi", "Opel", "BMW"]
for x,y in enumerate(cars):
    print(cars["{}".format(int(x))])
Error:
Exception has occurred: TypeError list indices must be integers or slices, not str File "C:\Users\stony\OneDrive\Stalinis kompiuteris\Python\smeliodeze.py", line 19, in <module> print(cars["{}".format(int(x))])
Reply


Messages In This Thread
Feed List items with Integer - by euras - May-17-2021, 04:57 PM
RE: Feed List items with Integer - by menator01 - May-17-2021, 05:21 PM
RE: Feed List items with Integer - by euras - May-18-2021, 09:22 AM
RE: Feed List items with Integer - by snippsat - May-18-2021, 09:37 AM
RE: Feed List items with Integer - by euras - May-18-2021, 10:18 AM
RE: Feed List items with Integer - by snippsat - May-18-2021, 11:22 AM
RE: Feed List items with Integer - by euras - May-18-2021, 02:17 PM
RE: Feed List items with Integer - by snippsat - May-18-2021, 02:48 PM
RE: Feed List items with Integer - by euras - May-19-2021, 07:30 AM
RE: Feed List items with Integer - by snippsat - May-19-2021, 07:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 190 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Draw bounding boxes on live feed Jerome 0 295 Jan-20-2024, 10:50 PM
Last Post: Jerome
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,341 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Finding combinations of list of items (30 or so) LynnS 1 883 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,609 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 2,532 May-26-2022, 01:37 PM
Last Post: Mark17
  how to assign items from a list to a dictionary CompleteNewb 3 1,586 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  How to parse a live feed in Python? Daring_T 2 4,124 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  Reading list items without brackets and quotes jesse68 6 4,636 Jan-14-2022, 07:07 PM
Last Post: jesse68
Question How to gather specific second-level items from a list chatguy 2 1,554 Dec-17-2021, 05:05 PM
Last Post: chatguy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020