Found 10476 Articles for Python

Convert list of strings and characters to list of characters in Python

Pradeep Elance
Updated on 20-May-2020 10:58:15

463 Views

While dalign with lists, we may come across a situation where we have to process a string and get its individual characters for further processing. In this article we will see various ways to do that.With list comprehensionWe design a for loop to go through each element of the list and another loop inside this to pick each character from the element which is a string.Example Live DemolistA = ['Mon', 'd', 'ay'] # Given lists print("Given list : ", listA) # Get characters res = [i for ele in listA for i in ele] # Result print("List of characters: ", res)OutputRunning ... Read More

Convert list of string into sorted list of integer in Python

Pradeep Elance
Updated on 20-May-2020 10:55:48

492 Views

Analysing data using python can bring us scenario when we have to deal with numbers represented as strings. In this article we will take a list which has numbers present as strings and we need to convert then to integers and then represent them in a sorted manner.With map and sortedIn this approach we apply the int function to every element of the list using map. Then we apply the sorted function to the list which sorts the numbers. It can handle negative numbers also.Example Live DemolistA = ['54', '21', '-10', '92', '5'] # Given lists print("Given list : ", listA) ... Read More

Convert list of numerical string to list of Integers in Python

Pradeep Elance
Updated on 20-May-2020 10:53:29

340 Views

For data manipulation using python, we may come across scenario where we have strings containing numbers in a list. To be able to make calculations, we will need to change the strings into numbers. In this article we will see the ways to change the strings into numbers inside a list.With intThe int function can be applied to the string elements of a list converting them to integers . We have to carefully design the for loops to go through each element and get the result even if there are multiple strings inside a single element.Example Live DemolistA = [['29', '12'], ... Read More

Convert dictionary object into string in Python

Pradeep Elance
Updated on 20-May-2020 10:51:52

361 Views

For data manipulation in python we may come across situation to convert a dictionary object into a string object. This can be achieved in the following ways.with str()In this straight forward method we simple apply the str() by passing the dictionary object as a parameter. We can check the type of the objects using the type() before and after conversion.Example Live DemoDictA = {"Mon": "2 pm", "Wed": "9 am", "Fri": "11 am"} print("Given dictionary : ", DictA) print("Type : ", type(DictA)) # using str res = str(DictA) # Print result print("Result as string:", res) print("Type of Result: ", type(res))OutputRunning the above ... Read More

Convert case of elements in a list of strings in Python

Pradeep Elance
Updated on 20-May-2020 10:48:40

3K+ Views

As part of data manipulation, we will come across the need to have a single case for all the letters in a string. In this article we will see how to take a list which has string elements with mixed cases. We then apply some python functions to convert them all to a single case.With lower()The lower function is a string function that can convert the entire string to lower case. So we use lambda and map to apply the lower function to each of the elements in the list.Example Live DemolistA = ['MoN', 'TuE', 'FRI'] # Given list print("Given list ... Read More

Convert byteString key:value pair of dictionary to String in Python

Pradeep Elance
Updated on 20-May-2020 10:47:43

973 Views

The byte string in python is a string presentd with letter b prefixed on it. In this article we will see how to convert a dictionary with the bytecode string into a normal dictionary which represents only strings.With decode and asciiPython string method decode() decodes the string using the codec registered for encoding. It defaults to the default string encoding. We use it to convert the bytecode value into normal asci values by supplying ascii as the parameter to the decode function.Example Live Demobstring = {b'day': b'Tue', b'time': b'2 pm', b'subject': b'Graphs'} print(bstring) # Use decode stringA = {y.decode('ascii'): bstring.get(y).decode('ascii') for ... Read More

Convert a string representation of list into list in Python

Pradeep Elance
Updated on 20-May-2020 10:43:38

14K+ Views

As python handles various data types, we will come across a situation where a list will appear in form of a string. In this article we will see how to convert a string into a list.With strip and splitWe first apply the strip method to remove the square brackets and then apply the split function. The split function with a comma as its parameter create the list from the string.Example Live DemostringA = "[Mon, 2, Tue, 5, ]" # Given string print("Given string", stringA) print(type(stringA)) # String to list res = stringA.strip('][').split(', ') # Result and its type print("final list", res) ... Read More

Convert a nested list into a flat list in Python

Pradeep Elance
Updated on 20-May-2020 10:41:15

682 Views

A nested list is a list whose elements are lists themselves. If we have a python data container which is a nested list, we may sometimes need to convert it into a flattened list so that each element can be processed further.Even the inner elements can also be nested themselves. And there can be many layers of nesting. So we will approach this problem with recursion. We will keep checking if the element is nested and then keep going applying the function again and again until the element is no longer a list. Once it is found that element is ... Read More

Convert a list of multiple integers into a single integer in Python

Pradeep Elance
Updated on 20-May-2020 10:38:55

3K+ Views

Sometimes we may have a list whose elements are integers. There may be a need to combine all these elements and create a single integer out of it. In this article we will explore the ways to do that.With joinThe join method can Join all items in a tuple into a string. So we will use it to join each element of the list by iterating through them through a for loop.Example Live DemolistA = [22, 11, 34] # Given list print("Given list A: ", listA) # Use res = int("".join([str(i) for i in listA])) # Result print("The integer is : ... Read More

Convert a list into tuple of lists in Python

Pradeep Elance
Updated on 20-May-2020 10:37:15

572 Views

Converting one data container into another in python is a frequent requirement. In this article we will take a list and convert into a tuple where each element of the tuple is also a list.With tupleWe can apply the tuple function straight to the list. But we have to also put a for loop in place so that each element is enclosed in a [].Example Live DemolistA = ["Mon", 2, "Tue", 3] # Given list print("Given list A: ", listA) # Use zip res = tuple([i] for i in listA) # Result print("The tuple is : ", res)OutputRunning the above code ... Read More

Advertisements