Python, How to concatenate list as index? - python

a=[1,2,3,4]
b=[5,6,7,8]
Desired outcome from some sort of concatenation.
c=[[1,2,3,4],[5,6,7,8]]
I need this to iterable as to concatenate a different b to c numerous times whilst retaining [[,,,],[,,,],[,,,],.......]

In python you can append anything you want into a list. So for your example, we will start with an empty list c and append a and b. This method will continue to work for an arbitrary number of lists:
a = [1,2,3,4]
b = [5,6,7,8]
c = []
# Now we append
c.append(a)
c.append(b)
If we wanted to do this manually, or as a once only for fixed numbers of lists, we could just defined c as the list containing a and b like so:
c = [a, b]

Related

Use values of a list as keys while adding other lists as values for each of the keys

I have 3 lists. I want to use the first that has 2 values as keys to a dictionary while adding the other 2 lists as values for each of the keys.
a = ['1','2']
b = ['3','4','5']
c = ['6','7','8']
expected result:
d = {1:['3','4','5'],2:['6','7','8']}
I've tried some combinations of dict(zip()) but wasn't able to achieve it. Thank you for any help.
By using zip(...) you can get the output you're looking for, but you'll need to create a list with b and c.
a, b, c = ['1','2'], ['3','4','5'], ['6','7','8']
d = dict(zip(a, (b,c)))
print(d)

I want to compare list of two cities and if list B cities are not present in list A then should append in list A using python

A=['mumbai','delhi','jaipur']
B=['mumbai','kolkata','pune']
I want to compare list A and list B and if list B cities are not in list A then we should able to append those cities in list A
One more way to do the same but not by using the for loop.
difference_between_lists = list(set(B) - set(A))
A += difference_between_lists
You loop through list B and check if the item in list B is in list A. If it is, don't do anything, if it isn't, append it to list A.
for i in B:
if not i in A:
A.append(i)
You can also do this using list comprehension
C = [i for i in B if not i in A]
print(A+C)
You can also use sets and do the union.
setA = set(A)
setB = set(B)
print(setB.union(setA))

Replacing a specific number of elements in one list with elements from another list

I had a question regarding list operations.
Let's say I had the following two lists:
a = [1,2,3,4,5,6]
and
b = [7,8,9]
If I was to input a as a value for one parameter within a function, and b as the second input within a function. How could I use indexing and slicing to have the elements in list b substitute (in this case) the values [4,5,6] so that the output would look like this:
Answer returned:
[1,2,3,7,8,9]
lets say x is the length of b
use slicing to remove the last x values of a.
than add b to a.
def put_in(a,b):
a = a[:len(a)-len(b)]
a = a+b
return a

Get a tuple from a list of tuple if the element occurs in a list of string

Hi I am working on a data transforming project. I have a List of tuples:
A = [("someThing",0),("someThingOnce",1),("someThingTwice",2)]
and an another list of string:
B = ["something","somethingonce","somethingagain"]
Now what I want to do is, I want the elements from list A that are present in list B.
The desired output is:
C = [("someThing",0),("someThingOnce",1)]
How can I achieve this in an optimised way since, list B has 7000 elements while list A has at max 20 elements.
I can't use numpy as the lists aren't of the same type, i..e B might contain numbers as well.
The tuple[0] in list A elements might repeat as well.
A list-comprehension is the most efficient solution for this (if A has less elements than B).
>>> A = [("someThing",0),("someThingOnce",1),("someThingTwice",2)]
>>> B = ["something","somethingonce","somethingagain"]
>>> C = [(i, j) for i, j in A if i.lower() in B]
>>> C
[('someThing', 0), ('someThingOnce', 1)]

Map the max function for list of the lists

I stack with the following problem, I need to finding maximum between equal positions between lists. Map function works pretty well, but how to make it work for the list of the lists? using map(max,d) gave the max of the every list. The problem is that the number of the lists in the list is variable. Any suggestions are welcome!
Input for the problem is d not an a,b,c, d - is a list of the lists, and the comparison is pairwise per position in the list.
a = [0,1,2,6]
b = [5,1,0,7]
c = [3,8,0,8]
map(max,a,b,c)
# [5,8,2,8]
d = [a,b,c]
map(max,d)
[6,7,8]
a = [0,1,2,6]
b = [5,1,0,7]
c = [3,8,0,8]
print [max(itm) for itm in zip(a, b, c)]
or even shorter:
print map(max, zip(a, b, c))
How about this:
max(map(max,d))

Categories

Resources