Python Concatenates List

I am new to python and it just automates something on my pc. I want to combine all the items in a list. The problem is that

''.join(list) 

will not work as this is not a list of strings.

This site http://www.skymind.com/~ocrow/python_string/ says the most efficient way to do this is

 ''.join([`num` for num in xrange(loop_count)]) 

but this is not valid python ...

Can someone explain the correct syntax for including this type of loop in string.join ()?

+10
source share
5 answers

You need to turn everything into a list into strings using the str() constructor:

 ''.join(str(elem) for elem in lst) 

Note that it is usually not recommended to use list for a variable name; it obscures the built-in list constructor.

I used a generator expression to apply the str() constructor to each item in the list. An alternative method is to use the map() function:

 ''.join(map(str, lst)) 

The reverse expressions in your example are another spelling of the repr() call to a value that differs substantially from str() ; you probably want the latter. Because it violates the Python principle, "There should be one - and only one is desirable - an easy way to do this." , backticks syntax has been removed from Python 3.

+15
source

Here is another way (Python 2.x discussion):

 ''.join(map(str, my_list)) 

This solution will have the fastest performance, and it looks beautiful and just imo. Using a generator will not be more efficient. Actually, it will be more efficient, since ''.join should allocate the exact amount of memory for the string based on the length of the elements, so in any case, you will need to use the entire generator before creating the string.

Note that `` was removed in Python 3, and it is not recommended to use it anymore, more explicitly, using str() if you need, for example. str(num) .

+5
source

just use this, don't need [] and use str(num) :

 ''.join(str(num) for num in xrange(loop_count)) 

for a list, simply replace xrange(loop_count) with the list name.

Example:

  >>> ''.join(str(num) for num in xrange(10)) #use range() in python 3.x '0123456789' 
+2
source

If your Python is too old for "list comprehension" (the odd [x for x in ...] syntax), use map() :

  ''.join(map(str, list)) 
+2
source

Lists in Python can be combined in two ways.

Using the operator + Using extends. Let list_a and list_b be defined as shown below

list_a = [1, 2, 3, 4]

list_b = [5, 6, 7, 8]

Using the + operator to combine two list_a and list_b

list_c = list_a + lis_b

print (list_c)

Output: [1, 2, 3, 4, 5, 6, 7, 8]

If we do not want a new list, i.e. list_c

list_a = list_a + list_b

print (list_a)

# Output: [1, 2, 3, 4, 5, 6, 7, 8]

Using the extension

An extension operator can be used to combine one list into another. Unable to save the value in the third list. One of the existing lists should store the combined result.

list_c = list_a.extend (list_b)

print (list_c)

Output: NoneType

print (list_a)

Output: [1, 2, 3, 4, 5, 6, 7, 8]

print (list_b)

Output: [5, 6, 7, 8]

In the above example, extends combines list_b into list_a

-one
source

All Articles