12

I want to transform the list ["A","B","A","A","B"] to the list ["AB","BA","AA","AB"].

I have tried to define a new list in which the first element is deleted and then add the strings of the lists together. After which I plan to delete the last element of the new list to get the result.

lista = sequences
lista.pop(0)
print(lista)

for x in range(sequences):
    mc =sequences[x]+lista[x]

But all I get is

TypeError: 'list' object cannot be interpreted as an integer

Any help is welcome.

Edit : Thank you guys, all your solutions worked perfectly :)

4 Answers 4

14

Best solution, using zip with a list comprehension, cleverest:

>>> l = ["A","B","A","A","B"]
>>> [x + y for x, y in zip(l, l[1:])]
['AB', 'BA', 'AA', 'AB']
>>> 

Or use an enumerate with a list comprehension:

>>> l = ["A","B","A","A","B"]
>>> [v + l[i + 1] for i, v in enumerate(l[:-1])]
['AB', 'BA', 'AA', 'AB']
>>> 
Sign up to request clarification or add additional context in comments.

Comments

13

Use zip():

>>> lst = ["A","B","A","A","B"]
>>> [x + y for x, y in zip(lst, lst[1:])]
['AB', 'BA', 'AA', 'AB']

Comments

8

You can use map():

s = list(map(str.__add__, lst[:-1], lst[1:]))

A bit better to use operator.concat() (thanks for advice, @MykolaZotko):

import operator

s = list(map(operator.concat, lst[:-1], lst[1:]))

Upd.

I've decided to do some tests on bigger data.

import operator

lst = [...] # list with 10000 random uppercase letters


def test1():
    return list(map(operator.concat, lst[:-1], lst[1:]))


def test2():
    return [x + y for x, y in zip(lst, lst[1:])]


def test3():
    return [v + lst[i + 1] for i, v in enumerate(lst[:-1])]


def test4():
    s = ''.join(lst)
    return [s[i:i + 2] for i in range(len(s) - 1)]


if __name__ == '__main__':
    import timeit
    print(timeit.timeit("test1()", setup="from __main__ import test1, lst", number=10000))
    print(timeit.timeit("test2()", setup="from __main__ import test2, lst", number=10000))
    print(timeit.timeit("test3()", setup="from __main__ import test3, lst", number=10000))
    print(timeit.timeit("test4()", setup="from __main__ import test4, lst", number=10000))

Results:

  1. Python 2:

    10.447159509
    11.529946446
    20.962497298000002
    20.515838672
    
  2. Python 3:

    10.370675522
    11.429417197
    20.836504865999995
    20.422865353
    

On bigger data map() is a bit (~9%) faster, but there's no significant difference between test1() and test2()

2 Comments

You can use operator.add or operator.concat as well. Strange that map is faster. Normally map is slower than listcomp.
@MykolaZotko, it's not faster, few miliseconds slower, almost the same. I've done a mistake while copying data
4

There are several issues in your original code:

sequences = ["A","B","A","A","B"]
lista = sequences
lista.pop(0)
print(lista)

for x in range(sequences):
    mc =sequences[x]+lista[x]

Firstly, the statement lista = sequences does not make a copy of sequences. Instead, lista and sequences become two different names for the same list. What you do using one name also happens to the other. lista.pop(0) is the same as sequences.pop(0). If you want a copy, then import the copy library.

import copy

sequences = ["A","B","A","A","B"]
lista = copy.copy(sequences)
lista.pop(0)
print(lista)

for x in range(sequences):
    mc =sequences[x]+lista[x]

Secondly, your statement range(sequences) is incorrect. The range() function accepts integers as input, not lists. That's what gave you TypeError: 'list' object cannot be interpreted as an integer

# VALID
range(5)
range(3)
range(10)

# INVALID
range(["A","B","A"])
range(["eyes", "nose", "tail"])

sequences is a list. You want range(len(sequences)) notrange(sequences)

In the end, we can modify your original code to work:

import copy

sequences = ["A","B","A","A","B"]
lista = copy.copy(sequences)
lista.pop(0)
print(lista) # prints ["B","A","A","B"]

mc = list()
for x in range(len(lista)):
    mc.append(lista[x] + sequences[x + 1])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.