How can I remove unnecessary square brackets Python [duplicate] - python

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 4 years ago.
For example, I'm having this:
a = [['1'], ['2'], ['3']]
How can I change it into:
a = ['1', '2', '3']
Thanks!

ooh, i'll play.
a = [i[0] for i in a]
(assuming you only have one item in each sublist)

Like so:
a = [['1'], ['2'], ['3']]
lst = []
for subLst in a:
lst.extend(subLst)
print(lst)
But in most cases this means that the generation of the list a get improved ...

Related

Hwo to convert array into Comma Saperated string in python? [duplicate]

This question already has answers here:
How to convert integers in list to string in python
(6 answers)
Closed 2 years ago.
I Have following Array in python,
[4,5,6]
i want my output as ['4','5','6']
How to Do that?
Use map to convert list of integer to list string:
arr = [4,5,6]
arr = list(map(str,arr))
arr
Result :
['4', '5', '6']
I suppose you want your integers represented as strings, so i suggest a list comprehension:
alpha = [4,5,6]
alpha = [str(i) for i in alpha]
print(alpha)
Output:
['4', '5', '6']

How to transfer range() output into string [duplicate]

This question already has answers here:
Generate string of numbers python
(2 answers)
Closed 4 years ago.
How to create a list of string using range() built-in function in python?
range(1,7) produces a range object equivilant to [1,2,3,4,5,6]
Desired list: ['1','2','3','4','5','6']
Use a cast to str() & list comprehension as per the following:
string_list = [str(x) for x in range(1, 7)]
With the map function:
list(map(str,range(7)))
Or the new f-strings:
>>> [f'{i}' for i in range(7)]
['0', '1', '2', '3', '4', '5', '6']
Use string formatting & list comprehension as per the following
string_list = ["{}".format(x) for x in 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.

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.

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

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

Categories