Python Forum
problem in using int() with a list of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in using int() with a list of strings
#1
hi
i am a beginner in python, so this problem that i encountered, maybe is silly.
i wrote in idle:

y=['l', '2', '3', '5', '8', '6', '9']
when i write int(y[0]) then i have an error as:
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
int(y[0])
ValueError: invalid literal for int() with base 10: 'l'

but when i write int(y[1]) or int(y[2]) , the correct values are displayed and there is no error. i searched net for this but i can not true thing.
thanks
Reply
#2
To which value should I (capital letter i) convert as integer?

It’s always a good practice to read error messages.
akbarza likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
It is pretty silly. Lowercase "L" is not a valid literal for int. Try replacing "l" with "1" (one).

Valuable lesson though. Do not use lowercase L as a variable name because it is difficult to distinguish from the 1 (one). For the same reason don't use uppercase "o" as a variable name.
akbarza likes this post
Reply
#4
(Jul-19-2023, 01:17 PM)akbarza Wrote: hi
i am a beginner in python, so this problem that i encountered, maybe is silly.
i wrote in idle:

y=['l', '2', '3', '5', '8', '6', '9']
when i write int(y[0]) then i have an error as:
Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
int(y[0])
ValueError: invalid literal for int() with base 10: 'l'

but when i write int(y[1]) or int(y[2]) , the correct values are displayed and there is no error. i searched net for this but i can not true thing.
thanks

Well im noob too but

1. Quotes around the number makes it a string and int is for math not strings
2. you can't just write int(y[0]) if you are trying to get the [0] placed string
3. you need to write string(y[0]) if you are trying to print I which is a letter and not an integer as far as I know
Reply
#5
(Jul-19-2023, 06:15 PM)Lahearle Wrote: 1. Quotes around the number makes it a string and int is for math not strings
2. you can't just write int(y[0]) if you are trying to get the [0] placed string
3. you need to write string(y[0]) if you are trying to print I which is a letter and not an integer as far as I know
Most of this is wrong, or I am misunderstanding what you wrote.
1. int(object, base) is a function for converting the object to an int. The object can be a number or a float. The OP is using int() to convert a str to an int. The problem is that the str is not something that can be converted to an int.

2. int(y[0]) is exactly how you would get the str y[0] and convert it to an int.

3. y[0] is a lowercase "L", not an uppercase "i", but it is easy to understand the mistake. Add uppercase "i" as being another bad variable name because it can easily be confused not only with the digit "1", but the lowercase letter "L".
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 483 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 775 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,554 May-01-2023, 09:06 PM
Last Post: deanhystad
  Help with Logical error processing List of strings dmc8300 3 1,093 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Problem with "Number List" problem on HackerRank Pnerd 5 2,126 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  Splitting strings in list of strings jesse68 3 1,780 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  How to find the first and last of one of several characters in a list of strings? tadsss 2 2,209 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Problem with replacing strings donnertrud 3 2,146 May-22-2020, 04:06 PM
Last Post: donnertrud
  Iterating through a list of strings Ash_Ren 1 2,095 Nov-22-2019, 08:30 PM
Last Post: buran
  How do I delete symbols in a list of strings? Than999 1 2,290 Nov-16-2019, 09:37 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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