Convert list to values python

I have a list

a = [3]
print a
[3]

I want ot convert it into a normal integer

print a
3

How do I do that?

asked Jan 28, 2010 at 5:01

1

a = a[0]
print a
3

Or are you looking for sum?

>>> a=[1]
>>> sum(a)
1
>>> a=[1,2,3]
>>> sum(a)
6

answered Jan 28, 2010 at 5:03

Convert list to values python

YOUYOU

116k32 gold badges184 silver badges216 bronze badges

The problem is not clear. If a has only one element, you can get it by:

a = a[0]

If it has more than one, then you need to specify how to get a number from more than one.

answered Jan 28, 2010 at 5:03

Alok SinghalAlok Singhal

89.8k19 gold badges124 silver badges156 bronze badges

1

I imagine there are many ways.

If you want an int() you should cast it on each item in the list:

>>> a = [3,2,'1']

>>> while a: print int(a.pop())

1
2
3

That would also empty a and pop() off each back handle cases where they are strings.

You could also keep a untouched and just iterate over the items:

>>> a = [3,2,'1']

>>> for item in a: print int(item)

3
2
1

answered Jan 28, 2010 at 6:33

brianraybrianray

1,1091 gold badge12 silver badges16 bronze badges

To unpack a list you can use '*':

>>> a = [1, 4, 'f']

>>> print(*a)

1 4 f

answered Jan 11, 2020 at 12:56

Convert list to values python

What Is a List in Python?

A list in python is an ordered sequence that can hold a variety of object types, such as, integer, character or float. A list in python is equivalent to an array in other programming languages. It is represented using square brackets, and a comma(,) is used to separate two objects present in the list.

A list and an array in other programming languages differ in the way that an array only stores a similar data type, meaning that an array is homogeneous in nature, but a list in python can store different data types at a time, and therefore it can either be homogeneous or heterogeneous. Below are some examples of homogeneous and heterogeneous lists in python:

Homogenous Lists:

HomogenousLists

Heterogeneous Lists:

HeterogeneousLists.

Accessing an Item From the List

AccessinganItem

An item from the list can be accessed by referring to its index in the list. The indexing of elements in the list starts from 0. Let’s take an example of the list we created in the last step.

To access an element from the list, we pass the index of that element in the print function.

As mentioned earlier, that indexing starts from 0, so when index [1] is passed, it gives the result as “dog”.  Similarly, if we pass the index, say [2], it will give the output 2.2

What Is a String in Python?

A string in python is an ordered sequence of characters. The point to be noted here is that a list is an ordered sequence of object types and a string is an ordered sequence of characters. This is the main difference between the two.

A sequence is a data type composed of multiple elements of the same data type, such as integer, float, character, etc. This means that a string is a subset of sequence data type, containing all elements as characters.

Here is an example of string in python and how to print it.

StringinPython

For declaring a string, we assign a variable to the string. Here, a is a variable assigned to the string simplilearn. An element of a string can be accessed in the same way as we saw in the case of a list. The indexing of elements in a string also starts from 0.

How to Convert a List to String in Python

  • Using Join Function

The join function is one of the simplest methods to convert a list to a string in python. The main point to keep in mind while using this function is that the join function can convert only those lists into string that contains only string as its elements. 

Refer to the example below.

ConvertListtoString_1

Here, all the elements in the list are individual string, so we can use the join function directly. Note that each element in the new string is delimited with a single space.

Now, there may be a case when a list will contain elements of data type other than string. In this case, The join function can not be used directly. For a case like this, str() function will first be used to convert the other data type into a string and then further, the join function will be applied. Refer to the example given below to understand clearly.

ConvertListtoString_2

  • Traversal of a List Function

In this example, firstly we declare a list that has to be converted to a string. Then an empty string has to be initialized to store the elements. After that, each element of the list is traversed using a for loop, and for every index, the element would be added to the initialized string. At the end, the string will be printed using the print() function.


ConvertListtoString_3

  • Using map() Function

The map function can be used in 2 cases to convert a list to a string. 

  1.  if the list contains only numbers.
  2. If the list is heterogenous

 The map() function will accept 2 arguments;

  1. str() function; that will convert the given data type into the string data type.
  2. An iterable sequence; each and every element in the sequence will be called by str() function. The string values will be returned through an iterator.

At the end, the join() function is used to combine all the values returned by the str() function.

ConvertListtoString_4

  • List Comprehension

List comprehension in python generates a list of elements from an existing list. It then employs the for loop to traverse the iterable objects in an element-wise pattern.

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

An example of conversion of list to string using list comprehension is given below.

ConvertListtoString_5

  • Iterating Through the List

In this method, Assign a list and traverse each element and add each into an empty string using for loop

The code in python is as given below

instr= ['Hello', 'How', 'are', 'you', 'doing?']

emptystr = ""

# passing in a string 

for i in instr:

emptystr += i +''

print(emptystr)

The output will be -  Hello How are you doing?

  • Using function that traverse each element of the list and keep adding new element for every index in empty string using for loop

# listToString is a function

def listToString(instr):

# initialize empty string

emptystr=""

# string traversal using for loop

for ele in instr: 

emptystr += ele

instr = ['Hello', 'How', 'are', 'you', 'doing?']

print(listToString(instr))

The output will be -  Hello How are you doing?

The article explains two of the data types List,and String in Python language and some ways of converting List to String in Python programming language. All methods have code that has readability and functions that are used often to convert List into String. It is important to understand each of the functions and its use, the syntax used and the developer should have hands-on experience with the expected effect on output using these functions. In all these methods iteration is used to move from one element to the next element in a list and then add each of the elements and convert them as a single string.

In case you wish to master the A to Z of Python, enroll in our Python Certification Training today! And in case you have any questions regarding the tutorial, drop a comment below and our experts will help you out.

How do I convert a list to a number in Python?

Use int() function to Convert list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.

How do you convert a list to a variable in Python?

# Python program to convert a list to a string by using the join() method..
# Creating a list with elements of string data type..
a_list = ["Python", "Convert", "List", "String", "Method"].
# Converting to string..
string = " ".join( a_list ) # this is read as join elements of a_list with a separator (" ").

How do I convert a list to text in Python?

How to Convert a String to a List of Words. Another way to convert a string to a list is by using the split() Python method. The split() method splits a string into a list, where each list item is each word that makes up the string.

Can we convert list to array in Python?

Lists can be converted to arrays using the built-in functions in the Python numpy library. numpy provides us with two functions to use when converting a list into an array: numpy. array()