Python Combine two list [duplicate] - python

This question already has answers here:
python nested for loop when index of outer loop equals to length of inner loop you start again
(6 answers)
Closed 10 months ago.
I have
A = ['A','B','C','D','E','F','G','H','J']
B= ['a','b','c']
I want to combine the list that 'a' is combine with first three element of list A , 'b' with the next three and 'c' with the last three as shown below
C = ['Aa','Ba','Ca','Db','Eb','Fb','Gc','Hc','Jc,]
how can I go about it in python

As a list comprehension you could do this. Although I suspect there's a nicer way to do it.
[f"{capital}{B[i//3]}" for i,capital in enumerate(A)]
i will increment by 1 for each letter in A so we can do floor division by 3 to only increment it every 3 iterations of A giving us the correct index of B and just use an f-string to concaninate the strings although capital + B[i//3] works too.

Related

Creating a list or range with all numbers up to a certain variable [duplicate]

This question already has answers here:
How do I create a list with numbers between two values?
(12 answers)
Closed 17 days ago.
So I am defining a function that takes in one variable R. Then I need to create a list of all integers from 0 to R (in the context of the problem R will always be positive).
EX) When I do
R=5
print(list(0,R))
I just get a list with 2 elements: 0 and 5, but I want 0,1,2,3,4,5
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
range(6) would return [0,1,2,3,4,5]

Lists in Python - Each array in different row [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
how can I create a new list with the following format.
The 3 arrays inside each row should be in different rows.
a= [['111,0.0,1', '111,1.27,2', '111,3.47,3'],
['222,0.0,1', '222,1.27,2', '222,3.47,3'],
['33,0.0,1', '33,1.27,2', '33,3.47,3'],
['44,0.0,1', '44,1.27,2', '4,3.47,3'],
['55,0.0,1', '55,1.27,2', '55,3.47,3']]
Final desired ouput:
b=[['111,0.0,1',
'111,1.27,2',
'111,3.47,3',
'222,0.0,1',
'222,1.27,2',
'222,3.47,3',
'33,0.0,1',
'33,1.27,2',
'33,3.47,3',
'44,0.0,1',
'44,1.27,2',
'44,3.47,3',
'55,0.0,1',
'55,1.27,2',
'55,3.47,3']]
Is this what you are looking for?
b = [[j for i in a for j in i]]
To be clear, there is no concept of rows vs. columns in Python. Your end result is just a big list of str's, within another list.
You can create the big list by chaining all of the original small lists together (a[0] + a[1] + ...), for which we may use
import itertools
big_list = list(itertools.chain(*a))
To put this inside another list,
b = [big_list]

Iterate over even indices in a list [duplicate]

This question already has answers here:
split list into 2 lists corresponding to every other element [duplicate]
(3 answers)
Closed 4 years ago.
Say i have this list:
CP = [1,0,1,0]
How can i print only 0's in the list via indices i.e CP[i].
Required output:
0
0
Any help would be highly appreciated!
This feels like a homework problem, and I assume you actually want odd indices instead of even indices.
Here's a possible solution:
# get a range of all odd numbers between 1 and length of list
# with a step size of 2
for i in range(1, len(CP), 2):
print(CP[i])
The one liner --
print ('\n'.join([str(CP[i]) for i in range(1, len(CP), 2)]))

Finding the index of an element of a list when you don't know how many occurrences of that element are in the list [duplicate]

This question already has answers here:
Find the index of the second occurrence of a string inside a list
(3 answers)
Find the index of the n'th item in a list
(11 answers)
Closed 7 years ago.
If I'm working with a list containing duplicates and I want to know the index of a given occurrence of an element but I don't know how many occurrences of that element are in the list, how do I avoid calling the wrong occurrence?
Thanks
I don't know that a single builtin does this thing alone, but you could fairly easily write it, for instance:
def index_second_occurence(alist, athing):
if alist.count(athing) > 1:
first = alist.index(athing)
second = alist[first + 1::].index(athing)
return second + first + 1
else:
return - 1

Find index of last item in a list [duplicate]

This question already has answers here:
How to find the last occurrence of an item in a Python list
(15 answers)
Closed 8 years ago.
For example, I have a list
[0,2,2,3,2,1]
I want to find the index of the last '2' that appears in this list.
Is there an easy way to do this?
You can try the following approach. First reverse the list, get the index using L.index().
Since you reversed the list, you are getting an index that corresponds to the reversed, so to "convert" it to the respective index in the original list, you will have to substract 1 and the index from the length of the list.
n = ...
print len(L) - L[::-1].index(n) - 1

Categories