[Python beginner] Join two lists alternately (5 lines).

Conclusion (alternately combined)

Combine list1 and list2


list1 = ['1', '2', '3']
list2 = ['hello', 'world']
result = [None]*(len(list1)+len(list2))
result[::2] = list1
result[1::2] = list2

print(result)#['1', 'hello', '2', 'world', '3']

image.png

Recommended Posts

[Python beginner] Join two lists alternately (5 lines).
[Python] Join two tables with pandas
[Python beginner] Divide one list (5 lines).
Beginner ABC154 (Python)
Beginner ABC156 (Python)
python beginner memo (9.2-10)
python beginner memo (9.1)
Python beginner notes
[Beginner] Python array
Beginner ABC155 (Python)
[Beginner] Python functions
Beginner ABC157 (Python)
PyQ ~ Python Beginner ~
Python beginner memo (2)
Python beginner Zundokokiyoshi
[Python beginner] Extract prefectures and cities from addresses (3 lines).
Memorize Python commentary 5 --Lists
Python3 | Lists, Tuples, Dictionaries
Python> Tuples versus Lists
Python lists, tuples, dictionaries