Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip two strings? [duplicate]

I have two lists:

country_name = ['South Africa', 'India', 'United States']
country_code = ['ZA', 'IN', 'US']

I want to group the country name and its corresponding code together and then perform a sort operation to do some processing

When I tried to zip these 2 lists, I am getting the 1st character from both the lists as the output.

I also tried doing this:

for i in xrange(0,len(country_code):
     zipped = zip(country_name[i][:],country_code[i][:])
     ccode.append(zipped)

to zip the entire string, but it didn't work. Also I am not sure that after zipping the 2 list, I will be able to sort the resulting list or not.

like image 674
Anurag Sharma Avatar asked Jul 30 '13 07:07

Anurag Sharma


People also ask

How do you zip two things together?

Right-click on the file or folder. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

How do you zip a string in Python?

Python's zip() function is defined as zip(*iterables) . The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.

How do you zip an array?

Array#zip() : zip() is a Array class method which Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument. Return: merges elements of self with corresponding elements from each argument.


2 Answers

You are using zip() wrong; use it with the two lists:

zipped = zip(country_name, country_code)

You are applying it to each country name and country code individually:

>>> zip('South Africa', 'ZA')
[('S', 'Z'), ('o', 'A')]

zip() combines the two input sequences by pairing off each element; in strings, the individual characters are the elements of the sequence. Because there are only two characters in the country codes, you end up with lists of two elements, each a tuple of paired characters.

Once you combined both lists into a new one, you can most certainly sort that list, either on the first or second element:

>>> zip(country_name, country_code)
[('South Africa', 'ZA'), ('India', 'IN'), ('United States', 'US')]
>>> sorted(zip(country_name, country_code))
[('India', 'IN'), ('South Africa', 'ZA'), ('United States', 'US')]
>>> from operator import itemgetter
>>> sorted(zip(country_name, country_code), key=itemgetter(1))
[('India', 'IN'), ('United States', 'US'), ('South Africa', 'ZA')]
like image 102
Martijn Pieters Avatar answered Sep 28 '22 14:09

Martijn Pieters


answer is in your question - use zip:

>>> country_name = ['South Africa', 'India', 'United States']
>>> country_code = ['ZA', 'IN', 'US']
>>> zip(country_name, country_code)
[('South Africa', 'ZA'), ('India', 'IN'), ('United States', 'US')]

If you have lists with different lengths, you could use itertools.izip_longest:

>>> from itertools import izip_longest
>>> country_name = ['South Africa', 'India', 'United States', 'Netherlands']
>>> country_code = ['ZA', 'IN', 'US']
>>> list(izip_longest(country_name, country_code))
[('South Africa', 'ZA'), ('India', 'IN'), ('United States', 'US'), ('Netherlands', None)]
like image 40
Roman Pekar Avatar answered Sep 28 '22 14:09

Roman Pekar