Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBastian Petersson
9,164 PointsIs there a way to make the list only accept one type?
Is there a way to make a list only accept one type like an array does in swift for example?
3 Answers
Cooper Runstein
11,850 PointsPython is a dynamically typed language, this sort of functionality doesn't exist in dynamic languages. You can check and make sure your list is all of the type you wanted, but you can't enforce it as, say, a list of integers.
Jared Mollenkamp
2,337 PointsYou could take your input ant convert it into an intager using int("7") and use the try command for if someone puts in a letter
Sergio Andrés Herrera Velásquez
Python Development Techdegree Graduate 12,968 Pointsyou can import from the typing module the List, it won't change the functioning or coerce the type ,but would allow you to type hint it to other developers or your future self , mostly used for documentation purposes as shown below
from typing import List def sum_integers(numbers: List[int]) -> int: total = 0 for number in numbers: total += number return total
Usage:
integers: List[int] = [1, 2, 3, 4, 5] result = sum_integers(integers) print(result) # Output: 15
Bastian Petersson
9,164 PointsBastian Petersson
9,164 Pointscool, thanks