Python Forum
Converting List into list of tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting List into list of tuples
#1
Here I am trying to convert a List into a dictionary. So, I am following this approach.
my_list = ['a', 'A', 'b', 'B', 'c', 'C']

If I convert a list into a multiple tuples (('a', 'A'), ('b', 'B'), ('c', 'C')), then I can easily convert into a dictionary using dict() built in function.

Please guide me to convert a list into multiples of tuples.
Reply
#2
Look into zip. I'd create an iterator from your list and zip it with itself, but if that sounds too advanced for you, consider slicing's third parameter to slice your list a couple of times into appropriate input for zip.
Reply
#3
If you go with zip(), you'll want to slice the list up into lowercase an uppercase characters to get the desired result.

my_list = ['a', 'A', 'b', 'B', 'c', 'C']
for pair in zip(my_list[::2], my_list[1::2]):
    print(pair)
Reply
#4
my_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E']
my_dict = dict([*zip(my_list[::2], my_list[1::2])])
print(my_dict)
Output:
{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E'}
A.D.Tejpal
Reply
#5
One can use zip() with iterator as well:

my_list = ['a', 'A', 'b', 'B', 'c', 'C']
dict(zip(*[iter(my_list)]*2))
Python built-in itertools module documentation contains following recipe:

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Adding values with reduce() function from the list of tuples kinimod 10 2,514 Jan-24-2023, 08:22 AM
Last Post: perfringo
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,714 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,500 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to check if a list is in another list finndude 4 1,793 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 3,966 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Use one list as search key for another list with sublist of list jc4d 4 2,107 Jan-11-2022, 12:10 PM
Last Post: jc4d

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020