Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
in a list or tuple
#3
Here's one way to go about it using try and except and then parsing the error that is returned.

tester1 = ('this', 'and', 'that')
tester2 = ['this', 'and', 'that']

def list_or_tuple (test_element) :
	try :
		test_element ['dummy']
	except TypeError as error:
		return (error.__str__ ().split (' ') [0])

print (f'tester1 [0] is "{tester1 [0]}".')
print (f'tester1 is a {list_or_tuple (tester1)}.\n')
print (f'tester2 [2] is "{tester2 [2]}".')
print (f'tester2 is a {list_or_tuple (tester2)}.')
Output:
tester1 [0] is "this". tester1 is a tuple. tester2 [2] is "that". tester2 is a list.
Reply


Messages In This Thread
in a list or tuple - by Skaperen - May-14-2021, 11:53 PM
RE: in a list or tuple - by bowlofred - May-15-2021, 12:09 AM
RE: in a list or tuple - by BashBedlam - May-15-2021, 01:15 AM
RE: in a list or tuple - by bowlofred - May-15-2021, 01:40 AM
RE: in a list or tuple - by Skaperen - May-16-2021, 05:28 PM
RE: in a list or tuple - by Yoriz - May-16-2021, 06:43 PM
RE: in a list or tuple - by Skaperen - May-16-2021, 09:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 533 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 527 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,760 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 2,025 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,321 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  Create SQLite columns from a list or tuple? snakes 6 8,864 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,508 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,909 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,665 Jul-29-2020, 12:02 AM
Last Post: Larz60+
  Arrange list of tuple using loop batchenr 7 3,563 Jun-16-2019, 03:24 PM
Last Post: Abdullah

Forum Jump:

User Panel Messages

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