How to prepend all list elements into another list [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Merge two lists in python?
How can I prepend list elements into another list?
A = ['1', '2']
B = ['3', '4']
A.append(B)
print A
returns
['1', '2', ['3', '4']]
How can I make it
['1', '2', '3', '4']?

A.extend(B)
or
A += B
This text added to let me post this answer.

list.extend, for example, in your case, A.extend(B).

this can also be done as
for s in B:
A.append(B)
of course A.extend(B) does the same work but using append we need to add in the above f

Related

Generating a list of consecutive numbers as strings [duplicate]

This question already has answers here:
Generate string of numbers python
(2 answers)
Closed 2 years ago.
I would like to generate a list of numbers such as this:
['1','2','3','4','5','6']
but all I can seem to get is:
[1, 2, 3, 4, 5, 6]
using this code:
values = list(range(1,7))
Is there some easy way to generate a list of consecutive numbers, that are within apostrophes, since I want the code to treat them like a string.
I have tried:
values = str(list(range(1,7)))
but that just gave me the same result as above.
>>> [str(i) for i in range(1,7)]
['1', '2', '3', '4', '5', '6']
The comprehension proposed by #CoryKramer has you covered, but given your attempts, you might be looking for:
>>> list(map(str, range(1, 7)))
['1', '2', '3', '4', '5', '6']
Try the following:
list(map(str, range(1,7)))

What does the "x for x in" syntax mean? [duplicate]

This question already has answers here:
Explanation of how nested list comprehension works?
(11 answers)
Closed 5 years ago.
What actually happens when this code is executed:
text = "word1anotherword23nextone456lastone333"
numbers = [x for x in text if x.isdigit()]
print(numbers)
I understand, that [] makes a list, .isdigit() checks for True or False if an element of string (text) is a number. However I am unsure about other steps, especially: what does that "x" do in front of for loop?
I know what the output is (below), but how is it done?
Output: ['1', '2', '3', '4', '5', '6', '3', '3', '3']
This is just standard Python list comprehension. It's a different way of writing a longer for loop. You're looping over all the characters in your string and putting them in the list if the character is a digit.
See this for more info on list comprehension.

Nesting List Comprehension [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 5 years ago.
I frequently run into a problem where I'm trying to make a list of lists of a certain length from a string.
This is an example where I have a string, but would like to make a list of lists of length 3:
x = '123456789'
target_length = 3
new = [i for i in x]
final = [new[i:i+target_length] for i in range(0, len(x), target_length)]
print(final)
Output:
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
So, this works, but feels so clunky.
Is there a better way to combine these arguments into one line or do you think that would make the code unreadable?
If you want to do it in one line you can just create the lists inside your comprehension:
x = '123456789'
target_length = 3
[list(x[i:i+target_length]) for i in range(0, len(x), target_length)]
>> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
This does it in one line:
f2 = [[x[(j * target_length) + i] for i in range(target_length)] for j in range(target_length)]

Access all elements of nested lists [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 years ago.
I am trying to find a short and elegant way in order to access all individual elements in nested lists. For example:
lst1 = ['1', '2', '3']
lst2 = ['4', '5']
outer = [lst1, lst2]
Is there a list comprehension that would return ['1', '2', '3', '4', '5']?
There are two short similar ways to do it:
import itertools
# with unpacking
list(itertools.chain(*outer))
# without unpacking
list(itertools.chain.from_iterable(outer))
import itertools
lst1 = ['1', '2', '3']
lst2 = ['4', '5']
outer = [lst1, lst2]
flattened = list(itertools.chain(*outer))
['1', '2', '3', '4', '5']

python looping through 2 lists skipping similar elements [duplicate]

This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 years ago.
I have 2 identical lists
a = [a1,a2,a3]
b = [a1,a2,a3]
What is a most effective way to iterate over these 2 list simultaneously while i am interesting only in combination of different elements from both lists despite order, i.e a1a2 and a1a3. Combinations a1a1, a2a2, a3a3, a2a1, a3a1 i am interesting to skip, but interesting keep iterators values avaliable.
Want to re phrase questions:
interesting in possible combinations of 2 elements from list a = [a1,a2,a3]
Use combinations,
from itertools import combinations
for i in combinations(['a1','a2','a3'],2):
print i
List comprehensions!
a = ['1', '2', '3']
b = ['1', '2', '3']
c = [i + j for i in a for j in b if j != i]
print(c) # prints -> ['12', '13', '21', '23', '31', '32']
EDIT
if you consider a1a2 and a2a1 to be duplicates you can use some clever slicing to skip them like so:
c = [ia + ib for i, ia in enumerate(a) for ib in a[i+1:]]
print(c) # prints -> ['12', '13', '23']
As you might notice, list b is not used in the second one.

Categories