Python Forum
Picking out integers from a non-list variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Picking out integers from a non-list variable
#1
Imagine there are two variables:

source=123456
selected=[]


Is there a way to append the first integer from the variable 'source' into the list variable 'selected'?

If not, then how can i separate all the integers in the variable 'source' and append them into the 'selected' list like this:

>>>print(selected)
>>>['1','2','3','4','5','6']


Thanks for any responses.
Reply
#2
>>> source = 123456
>>> list(str(source))
['1', '2', '3', '4', '5', '6']
>>> str(source)[0]
'1'
>>>
Reply
#3
You can transform an integer into a string with str(integer_to_convert_to_string).
You can then iterate through each character of the string.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mediapipe. Not picking up second variable stevolution2024 1 173 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,156 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,364 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,444 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  Error "list indices must be integers or slices, not str" dee 2 1,454 Dec-30-2022, 05:38 PM
Last Post: dee
  read a text file, find all integers, append to list oldtrafford 12 3,516 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 1,295 Jul-11-2022, 08:52 AM
Last Post: japo85
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,570 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  An IF statement with a List variable dedesssse 3 8,207 Jul-08-2021, 05:58 PM
Last Post: perfringo
  User input/picking from a list AnunnakiKungFu 2 2,326 Feb-27-2021, 12:10 AM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

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