

Val myFriends = List("Adam", "David", "Frank") In a social-networking application, you might do the same thing with a list of friends, and their friends:
#CONVERT STRING TO INT PYTHON LIST OF LISTS CODE#
(Imagine trying to write that code with only a for loop.) This helps to demonstrate the power of the Scala collections methods. People: List = List(Al, Julia, Kim, Terry) Scala> val people = (_.capitalize).sorted If you really want to have fun, capitalize each element in the resulting list and then sort the list: People: List = List(kim, al, julia, terry) Scala> val couples = List(List("kim", "al"), List("julia", "terry"))Ĭouples: List] = List(List(kim, al), List(julia, terry)) In the real world, you might use flatten to convert a list of couples attending a wedding into a single list of all people attending the wedding. Though I use the term “list” here, the flatten method isn’t limited to a List it works with other sequences (Array, ArrayBuffer, Vector, etc.) as well: Lol: List] = List(List(1, 2), List(3, 4))Ĭalling the flatten method on this list of lists creates one new list:Īs shown, flatten does what its name implies, flattening the lists held inside the outer list into one resulting list. To demonstrate this, first create a list of lists: Use the flatten method to convert a list of lists into a single list. You have a list of lists (a sequence of sequences) and want to create one list (sequence) from them.


This is Recipe 10.15, “How to Flatten a List of Lists in Scala with flatten” Problem This is an excerpt from the Scala Cookbook (partially modified for the internet). show more info on classes/objects in repl.Some values in the input list can't be converted to int. Print("Some values in the input list can't be converted to int.") To handle exceptions so that the program doesn’t terminate abruptly, you can use python try-except blocks as shown below. It may result in loss of data or work done in your program. Due to this, the program runs into a ValueError exception.Įxceptions cause a program to terminate abruptly. In the above example, the string ‘ Aditya‘ cannot be converted into an int. ValueError: invalid literal for int() with base 10: 'Aditya' Output: Traceback (most recent call last):įile "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 2, in You can observe this in the following example. Therefore, it is possible that the program may run into the ValueError exception if we find an element in the list that cannot be converted into an integer. This approach doesn’t check if the string can be converted into an int or not before calling the int() function. You can use list comprehension instead of for loops to convert a list of strings to a list of ints as shown below. List comprehension in python is used to create new lists from existing container objects. Convert a List of Strings to Integers Using List Comprehension Hence, it has been omitted from the output. In the above example, the string ‘Aditya’ cannot be converted into an int. 'Aditya' cannot be converted into an integer. Print("data type of ' cannot be converted into an integer.".format(element)) The int() function takes a string or a floating-point literal as its input argument and returns an integer as shown below. Convert a List of Lists of Strings to Ints Inplace Using eval() Function.Convert a List of Lists of Strings to Ints Inplace Using int() Function.Convert a List of Lists of Strings to Ints Inplace in Python.Convert a List of Strings to Ints Inplace Using eval() Function.Convert a List of Strings to Ints Inplace Using the int() Function.Convert a List of Strings to Ints Inplace in Python.Convert a List of Lists of Strings to Ints Using the eval() Function.Convert a List of Strings to Ints Using the eval() Function.

Convert a List of Lists of Strings to Ints Using the map() Function.Convert a List of Strings to Ints Using the map() Function.Convert a List of Strings to Integers Using List Comprehension.Convert a List of Lists of Strings to Ints Using for Loop in Python.Convert a List of Strings to Ints Using for Loop in Python.
